Background : I have a few different threads which each need to write to a log file (.txt). i need a way of sharing this file between the threads but i’m not sure how to go about doing this, i’ve read into perhaps using some sort of queue, and polling this queue to push the messages into the text file and such. Or do i need to lock files and just get the messages in a queue to be written after the one infront has finished?
Currently i’m writing like so;
fileToWrite = new System.IO.StreamWriter(DeviceManager.logPath + correctDateTimeFormat);
But i’m getting the error message;
The process cannot access the file 'filename' because it is being used by another process.
i can only assume multiple threads are trying to access this at the same time.
Can anyone point me in the right direction as to what approach i should take with this?
Those are 2 different problems you are facing here with.
This FileStream constructor with
FileSharingparameter can help you with opening files from few threads, before closing them.However, for your need, it would be better if you would centralize access to your file (for example, log file) in one place (make a class, that have one instance which will control access to this particular file) and make sure, it will synchronize access from different threads by locking some private mutex, so you will not write to file from few threads simultaneously.
Very simple example, which needs building up:
Once again, it is very simple, only to show basic rule of what we are trying to achieve here.
Few other options you have here: