简体   繁体   中英

C# — Safe Multiple Thread Save File Access

I am currently developing a C# application that uses two separate threads , the first is the main thread which the program operates on and the second is the log thread . The log thread is basically a loop that runs every second or so and breaks only when the application is being closed.

Basically, every second or so, the log thread writes to a log file (text file) if there are messages to output. My problem is, on the main thread I have a move function which can change the path to the log file (and other files) and it moves the old files to the new location. My problem is, what happens if the log thread is writing to the log file and the move function tries to move the file or vice versa? And how would I help prevent this from happening?

The program runs fine if I don't change the save path but I am worried that the main thread and log thread may become deadlocked if they both try to do something to the log file at the same time. I've done some research into this but all I can come up with are ways to stop multiple threads from reading/writing to a file at the same time, not moving the file and writing to it like I am. So really, is there some way that the log thread can tell the main thread it is using the log file and the main thread can use the log file when it is done or vice versa?

Multi threading is something I have always wanted to try but was always put off by the complexity of things like this. It certainly requires much more logic to stop things from going wrong.

Help would be greatly appreciated as this is one of the last things that the application needs in order to prevent massive errors (at least that I can tell, that's what testing is for really).

Edit: What I am really looking for is some way that the two threads can "communicate" to tell each other when they are accessing the log file so that they don't try to access the same file at the same time.

每当主线程需要复制文件时,我就会一起杀死你的日志线程,然后再启动一个新的日志线程。

Your question guides this in the wrong direction, you might get a better answer if you explain what your end goal is.

Usually this is done differently. Multiple threads log to an in memory buffer. Each thread has its own buffer, so there is no buffer contention. When the buffer of a thread is full, a new buffer is created and swapped with the full buffer. Logging continues to the new buffer, while the old buffer is sent to a thread whose purpose is just to write buffers to files and rotate the log files. This allows multiple threads to log in memory very fast. Since it's only one thread that actually interacts with the file, there's no conflict around the file movement operation.

Regarding your question, assuming file moving does not happen often, you could use a slim multiple reader, single writer lock https://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim%28v=vs.110%29.aspx to achieve that (if you have multiple threads logging and only one moving the file), otherwise if you have just two threads, a plain lock(obj) statement will suffice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM