Asynchronous Process.WaitForExit()?

  • Thread starter Thread starter Douglas Harber
  • Start date Start date
D

Douglas Harber

Is there any way to do an asynchronous wait for a process to exit? My
application needs to monitor when processes it launched have completed and
take further action. However, the app can launch processes essentially
randomly (with respect to any other launched processes and/or their
completion). The current implementation has to create a thread per process
simply to wait for that process to exit and fire an event back into the main
thread. It would be a whole lot simpler if there was a BeginWaitForExit()
method on Process to allow my app to be notified asynchronously as each
process terminated.

Is there any way to emulate that kind of functionality? So far, I'm missing
it.

In fact, it's not clear to me that you can asynchronously wait on any
signalable Win32 object in the elegant generalized fashion of Win32.

Am I overlooking some functionality that's available in .NET?

BTW, it's fine if it'a .NET 2.0 only thing.

Thanks,
Doug Harber
 
Douglas Harber said:
Is there any way to do an asynchronous wait for a process to exit? My
application needs to monitor when processes it launched have completed and
take further action. However, the app can launch processes essentially
randomly (with respect to any other launched processes and/or their
completion). The current implementation has to create a thread per process
simply to wait for that process to exit and fire an event back into the
main thread. It would be a whole lot simpler if there was a
BeginWaitForExit() method on Process to allow my app to be notified
asynchronously as each process terminated.

Is there any way to emulate that kind of functionality? So far, I'm
missing it.

Take a look at ThreadPool.RegisterWaitForSingleObject. This uses a single
thread pool thread to wait on up to 64 handles at once, notifying you via
callback when the object is signalled or a timeout occurs.
In fact, it's not clear to me that you can asynchronously wait on any
signalable Win32 object in the elegant generalized fashion of Win32.

You can't. Win32 waits are strictly synchronous - any semblance of an async
wait is accomplished with the help of a worker thread.
Am I overlooking some functionality that's available in .NET?

ThreadPool.RegisterWaitForSingleObject has been available since 1.0.

-cd
 
| Is there any way to do an asynchronous wait for a process to exit? My
| application needs to monitor when processes it launched have completed and
| take further action. However, the app can launch processes essentially
| randomly (with respect to any other launched processes and/or their
| completion). The current implementation has to create a thread per process
| simply to wait for that process to exit and fire an event back into the
main
| thread. It would be a whole lot simpler if there was a BeginWaitForExit()
| method on Process to allow my app to be notified asynchronously as each
| process terminated.

Another thread for starting each process is the best way IMO. At the end of
WaitForExit(), do a xInvoke() back to the UI thread.
 
Back
Top