Hi,
I have this situation:
A worker thread creates and starts a process and then it needs to wait for
it to exit.
Now, its possible that the thread to be aborted using the Thread.Abort().
I know this is not the recommended way but for some reasons I cannot change
this behaviour now.
The problem is how to wait for the process exit taking into account the
thread abort.
if the thread is using
process.WaitForExit();
the Abort() call wont return until the process exits.
this is because the WaitForExit() I think what it does is to "make an
unmanaged call into the operating system that has blocked the thread in
unmanaged code" (quoted from MSDN).
http://msdn2.microsoft.com/en-us/library/74169f59.aspx
(Blocking issues)
An idea would be to do this:
WaitHandle waitForExit = new WaitHandle();
waitForExit.Handle = process.Handle;
waitFoExit.WaitOne();
the MSDN says at Handle Process property that it could be used to intialize
a WaitHandle and the "Blocking Issues" section suggest that
WaitHandle.WaitOne() is a recommended way to wait in order for the
Thread.Abort() to interrupt the thread.
what do you think ?
andrew - 26 Jul 2007 17:50 GMT
in fact it should be like this:
WaitHandle[] waitForExit = new WaitHandle[1];
waitForExit[0].Handle = process.Handle;
waitForExit[0].WaitOne();
since WaitHandle ctor is protected.