Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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*/
    }
share|improve this question

put on hold as off-topic by Bart van Ingen Schenau, Snowman, GlenH7, gnat, Kilian Foth yesterday

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Welcome to the world of multithreaded programming! You're going to need a lock, aka semaphore, to do what you want. – Florian Margaine Jan 30 at 14:00
    
@FlorianMargaine, what? everything you said almost went over the head. Explain please :) – AKC Jan 30 at 14:01
    
They're terms you should be able to Google :-). – Florian Margaine Jan 30 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. – AKC Jan 30 at 14:04
    
Alternatively, make PooledProc() synchronized. – Lawrence Jan 30 at 14:12
up vote 3 down vote accepted

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.

share|improve this answer
    
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? – AKC Jan 30 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. – Alexei Jan 30 at 16:52
    
Thanks man your suggestion worked. I am using the very first one (ReaderWriteLock) – AKC 2 days ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.