问题描述
我要上2个不同类型的WaitForMultipleObjects的:
I want to WaitForMultipleObjects on 2 different types:
- 一个的EventWaitHandle
- 一Process.Handle'==>的IntPtr
我不知道如何转换(在适当的方式)process.Handle ,以便有以下代码工作的WaitHandle一:
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' ...
任何人都知道正确的方式来等待一个过程和的EventWaitHandle?
Anybody know the right way to wait for a process and an EventWaitHandle ?
更新的原因...我的选择答案所有感谢所有的
第一:Jaroen,Slugart和斯利拉姆。所有的答案都非常好。
First of all thanks to all: Jaroen, Slugart and Sriram. All answers were very nice.
- 的原因,我忽略了我的机器上没有工作Jaroen解决方案。我的已退出事件从未发生(或许只有在设置?)。
- Slugart解决方案完美地工作,我尝试它之前,我红了答案。
- 斯利拉姆解决方案完美地工作,我选择了它,因为我没有创建一个虚假的EventWaitHandle,似乎是根据我的视野更干净。
感谢很多!
推荐答案
您可以的代表 Process.Handle
,并使用该实例的WaitHandle
等
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);
这篇关于如何等待第2组成:一个处理和的EventWaitHandle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!