4

I have got a text file called "vholders.txt".

I am making multiple threads as you can see here ,those threads work with their own given data and at last they write their own output to the vholders.txt. But I get IO exception cause file is being used by another thread. So how can I write to vholders.txt file without colliding with other threads.The sequence of which thread should write first doesn't matter.

this is my code:

public void execute() { for(int x=0;x<entered_length;x++) { ThreadPool.QueueUserWorkItem(new WaitCallback(PooledProc),x); } } private void PooledProc(object x_) { string output = string.Empty; //does the processing...and assign output its value... /*this is where I get error*/ StreamWriter sw = File.AppendText("vholders.txt"); //error, file is being used by another process sw.WriteLine(output); sw.Close(); /*Now how can I write the output value to the text file vholders.txt without getting IO Exception*/ } 
5
  • 3
    Welcome to the world of multithreaded programming! You're going to need a lock, aka semaphore, to do what you want. Commented Jan 30, 2016 at 14:00
  • @FlorianMargaine, what? everything you said almost went over the head. Explain please :) Commented Jan 30, 2016 at 14:01
  • They're terms you should be able to Google :-). Commented Jan 30, 2016 at 14:02
  • okie dokie!, plus if you know any answer to my question or modify my question to a better looking question then that would be great. Commented Jan 30, 2016 at 14:04
  • Alternatively, make PooledProc() synchronized. Commented Jan 30, 2016 at 14:12

1 Answer 1

7

In C# you can use ReaderWriteLock class to allow a single write operation in your file (and multiple readers, if this is needed).

Also, to maximize performance you can have asynchronous operations using Asynchronous File I/O (you can have some processing while the I/O operations are being done).

However, before diving into these concepts, some things must be clarified in order to obtain the simplest solution for your problem:

  1. do you have any readers from your file while it is being written?
  2. is this a continuous process or it happens just several times per day?
  3. are you processing large data (GB?)

If the answer is No to all the above questions, you might consider doing all the processing in memory and writing all the output at once.

3
  • To all your questions,the answer is no, but sometimes output can size upto MBs thats not a problem but I am trying to keep memory usage least.So, what should I do? Commented Jan 30, 2016 at 16:33
  • As already suggested in a previous comment use some locking mechanism, so that 2+ threads do not write in your file in the same time. I have suggested ReaderWriteLock class, as it seems to be designed exactly for what you need. Commented Jan 30, 2016 at 16:52
  • Thanks man your suggestion worked. I am using the very first one (ReaderWriteLock) Commented Feb 2, 2016 at 1:19