I want to WaitForMultipleObjects on 2 different types:
I don't know how to convert (in the appropriate way) "process.Handle" to a WaitHandle in order to have the following code working:
var waitHandles = new WaitHandle[2];
waitHandles[0] = waitHandleExit;
// Next line is the offending one:
waitHandles[1] = new SafeWaitHandle(process.Handle, false);
int waitResult = WaitHandle.WaitAny(waitHandles, timeOut);
Im getting the error:
Error 1 Cannot implicitly convert type 'Microsoft.Win32.SafeHandles.SafeWaitHandle' to 'System.Threading.WaitHandle' ...
Anybody know the right way to wait for a process and an EventWaitHandle ?
Update... Reasons for my choice of answer.
First of all thanks to all: Jaroen, Slugart and Sriram. All answers were very nice.
Thanks a lots!!!
You could subclass the WaitHandle which represents Process.Handle
and use instance of that WaitHandle
to wait.
public class ProcessWaitHandle : WaitHandle
{
private readonly Process process;
public ProcessWaitHandle(Process process)
{
this.process = process;
this.SafeWaitHandle = new SafeWaitHandle(process.Handle, false);
}
}
var waitHandles = new WaitHandle[2]
{
waitHandleExit,
new ProcessWaitHandle(process)
};
int waitResult = WaitHandle.WaitAny(waitHandles, timeOut);