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*/
}