Hi bonk,
As I understood from your requirement. There are two key problems you want
to solve.
1. Write to a file from multiple threads in the same process
2. Write to a file from multiple threads from multiple processes
The 1st problem can be solved quite easily. But the second problem may need
a re-thinking on your end to see if the approach itself is correct. I say so
because writing to the same file from multiple process without any mediating
process is difficult to manage. The main issue would be to synchronize the
access to the file by multiple processes so that the final file content won't
look as garbage data due to mix up of data from multiple processes. And
synchronizing actions from multiple processes will un-doubtedly and severly
degrade the performance of the application. Probably if you want to use the
same file name to write content then you could as well think of creating one
file per process with the following format.
<filename>_<pro cess-id>.<ext>
Anyway coming to the 1st problem (Write to a file from multiple threads in
the same process), here also you need to synchronize the writing of data to
your FileStream class from mutiple threads so that the data won't get
mixed-up. For this you can use lock on the FileStream object representing
your file inside your "Write" method. This technique uses locks and may still
pose performance issues. I have created some free C# libraries that can
assist in performing such operations without causing performance degradation.
Take a look at my recent library that I posted on CodeProject.com
http://www.codeproject.com/cs/librar...codeblocks.asp
Using this library you can implement the Write method of your FileStream as
shown below (only Pseudo code).
class AdvancedFileStr eam
{
int AdvancedFileStr eam::Write(byte[] data,....)
{
new async(delegate { this.InternalWr ite(data,...) }, _myThreadPool);
}
Sonic.Net.Threa dPool _myThreadPool = new Sonic.Net.Threa dPool(1,1);
}
You can use one ThreadPool (defined in my library) with one maximum and one
concurrent thread to do all writing of the data to the underlying file stream
represented by the AdvancedFileStr eam object. When multiple threads call into
Write the method will post a delegate to the _myThreadPool and will return
immediately to the calling code. Later the delegate will be executed on the
ThreadPool thread dedicated for this instance of the AdvancedFileStr eam
object. Since there is only one thread in this ThreadPool only one write
request will be handled at any given point of time thus protecting the
sequence of the writes to the file from multiple threads.
There is one issue that you need to be aware of in this approach. The byte
array supplied to the AdvancedFileStr eam::Write method should not be re-used
as we do not know when the ThreadPool thread gets a chance to perform the
actual write. There are ways to overcome it. I leave this to you as you can
figure out several options after reading my article on ACB.
Hope this helps.
--
Regards,
Aditya.P
"bonk" wrote:
I am trying to create a stream that writes text to a file and:
- automatically creates a new file once the current file exceeds a
certain size
- makes it possible to be used by multiple threads and/or processes so
that multiple threads/processes can write to the same file (all threads
use the same instance of the stream, processes use a different instance
but still may point to the same file)
Could you point me in the correct direction how this would ideally be
implemented? What class should I drive from? How would I solve the
problem of multiple threads acessing the same stream or multiple
processes acess the same file?
Additional information that might be helpful: The stream will be used
with TraceListener (passed in the ctor of the TraceListener).