create and wait for multiple processes

C

colson

I know how to create a new process using System.Diagnostics.Process and
wait for the process to end using WaitForExit(). How do I wait for
multiple processes? Is there an equivalent to WaitForMultipleObjects
under .NET?

Thanks
 
M

Michael Nemtsev

Hello colson,

See MSDN for WaitHandle.WaitAny() method

c> I know how to create a new process using System.Diagnostics.Process
c> and wait for the process to end using WaitForExit(). How do I wait
c> for multiple processes? Is there an equivalent to
c> WaitForMultipleObjects under .NET?
c>
c> Thanks
c>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
W

Willy Denoyette [MVP]

|I know how to create a new process using System.Diagnostics.Process and
| wait for the process to end using WaitForExit(). How do I wait for
| multiple processes? Is there an equivalent to WaitForMultipleObjects
| under .NET?
|
| Thanks
|

Yes there is an equivalent but it's not applicable here,
WaitForMultipleObjects waits for kernel objects to be signaled, while
WaitForExit waits for a processes to exit. All you need to do is create a
separate thread for each process you need to start and wait for.


Willy.
 
B

Ben Voigt

Willy Denoyette said:
|I know how to create a new process using System.Diagnostics.Process and
| wait for the process to end using WaitForExit(). How do I wait for
| multiple processes? Is there an equivalent to WaitForMultipleObjects
| under .NET?
|
| Thanks
|

Yes there is an equivalent but it's not applicable here,
WaitForMultipleObjects waits for kernel objects to be signaled, while
WaitForExit waits for a processes to exit. All you need to do is create a
separate thread for each process you need to start and wait for.

Yeeech. Processes are kernel objects, and WaitForMultipleObjects handles
them just fine.

WaitHandle.WaitAll would work if Process had a WaitHandle property. But it
doesn't, and the ProcessWaitHandle class is internal to System.dll.
However, if you can get a WaitHandle from somewhere (WaitHandle is abstract
so new WaitHandle() won't work), you can reassign its Handle property to the
Process object's Handle property, and then use WaitHandle.WaitAll.

Or get the process objects' Handle properties, stuff 'em into an array, and
p/invoke WaitForMultipleObjects.

http://pinvoke.net/default.aspx/kernel32/WaitForMultipleObjects.html
 
W

Willy Denoyette [MVP]

|
| | >
| > | > |I know how to create a new process using System.Diagnostics.Process and
| > | wait for the process to end using WaitForExit(). How do I wait for
| > | multiple processes? Is there an equivalent to WaitForMultipleObjects
| > | under .NET?
| > |
| > | Thanks
| > |
| >
| > Yes there is an equivalent but it's not applicable here,
| > WaitForMultipleObjects waits for kernel objects to be signaled, while
| > WaitForExit waits for a processes to exit. All you need to do is create
a
| > separate thread for each process you need to start and wait for.
|
| Yeeech. Processes are kernel objects, and WaitForMultipleObjects handles
| them just fine.

Sorry, bad wording, I didn't mean to suggest that processes aren't kernel
objects.
|
| WaitHandle.WaitAll would work if Process had a WaitHandle property. But
it
| doesn't, and the ProcessWaitHandle class is internal to System.dll.

That's what I meant when I said it's not applicable here, there is no
publically exposed WaitHandle, 'would work if .. " isn't very usefull here.


| However, if you can get a WaitHandle from somewhere (WaitHandle is
abstract
| so new WaitHandle() won't work), you can reassign its Handle property to
the
| Process object's Handle property, and then use WaitHandle.WaitAll.
|

Sure you can do this by grabing the process handle, but it isn't that simple
as it may look like if you want to make it safe and solid. Just look at the
WaitForExit to get an idea of what (partly) should be done.

| Or get the process objects' Handle properties, stuff 'em into an array,
and
| p/invoke WaitForMultipleObjects.
|
| http://pinvoke.net/default.aspx/kernel32/WaitForMultipleObjects.html
|

No, thanks, I stay with my suggestion - one thread per process (I suppose
the OP isn't going to start hundreds of process that way), I prefer not to
go down a PInvoke hack when I don't have to.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top