views:

209

answers:

2

hi i created a process in C# to execute an external program, i used the asynchronous methods to read on the standardoutput and all is working fine. However i have an issue, i'm printing a line indicating the process has finished. The problem is that, some times it may so happen when the data in the buffer is huge that, the process may exit but as the printing is happening on other thread, it may still be printing the data. So the information indicating that the process has terminated shows up before the whole output is printed. Is there any thing provided in .NET to resolve such an issue? since i'm making a asynchronous call so i'm unable to access process.standardoutput value since it raises a conflict. What can i do?

+2  A: 

Use synchronization between your threads !

You should look into AutoResetEvents, which perfectly fit your scenario (ie a thread wants to notify another one that an event as occured - in your exemple, that the buffer read is over)

Typically, your main thread will wait on the AutoResetEvent, which will be pulsed by the worker tread once its work is done.

In this specific case (ie waiting for a thread to finish), you can also use Thread.Join, which will block your main thread until your worker thread is terminated

Brann
i will look into it.thanks..will let you know what happens.
Anirudh Goel
+1  A: 

If you call EndInvoke on the async delegate, the thread should block until the operation has completed.

Lee