japreiss
japreiss

Reputation: 11251

Win32 equivalent of .NET's System.Diagnostics.Process.Exited event?

In .NET, you can subscribe to the Exited event on a System.Diagnostics.Process object and get asynchronous notification when the process exits. This is really convenient for me.

Is there any equivalent in straight Win32 using signals or something? The closest thing I an imagine is spawning a thread that does nothing but WaitForSingleObject on the process handle with infinite timeout, and then call a callback.

Upvotes: 1

Views: 342

Answers (2)

Hans Passant
Hans Passant

Reputation: 941317

Yes, a thread that calls WaitForSingleObject() on the process handle will work. But that's wasteful, you can get the operating system to do that for you with RegisterWaitForSingleObject(). The exact same function that the .NET Process class uses to raise the Exited event.

Exact same kind of constraints in what you can do in the callback, it runs on an arbitrary threadpool thread so you have to properly interlock where necessary to keep your code thread-safe.

Upvotes: 2

yakiro
yakiro

Reputation: 763

The last argument of CreateProcess is PROCESS_INFORMATION struct which contains hProcess handle, you can use this handle with WaitForSingleObject

Upvotes: 0

Related Questions