[英]WaitForSingleObject return ERROR_INVALID_HANDLE
I'm trying to determine if a process is still alive (at the moment I did check, I'm aware it can be closed right after I do the check) by calling WaitForSingleObject()
with a handle get from Process.MainWindowHandle
which works just fine with IsIconic()
but it return WAIT_FAILED
and GetLastError()
a ERROR_INVALID_HANDLE
我试图通过调用带有
Process.MainWindowHandle
有效的Process.MainWindowHandle
WaitForSingleObject()
来确定进程是否仍在运行(在我进行检查的那一刻,我知道它可以在执行检查后立即关闭)。与IsIconic()
很好,但是它返回WAIT_FAILED
和GetLastError()
一个ERROR_INVALID_HANDLE
UInt32 r = WaitForSingleObject(handle, 0);
if(r == WAIT_OBJECT_0)
{
MessageBox.Show("still running!");
}
if(r == WAIT_FAILED)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
You cannot wait on a window handle. 您不能等待窗口句柄。 You can pass window handles to window-related functions, like
IsIconic()
, but they are not kernel objects so you cannot wait on them. 您可以将窗口句柄传递
IsIconic()
窗口相关的函数,例如IsIconic()
,但是它们不是内核对象,因此您不能等待它们。 The documentation gives a list of objects that you can wait on: 该文档提供了您可以等待的对象列表:
The WaitForSingleObject function can wait for the following objects:
WaitForSingleObject函数可以等待以下对象:
- Change notification
变更通知
- Console input
控制台输入
- Event
事件
- Memory resource notification
内存资源通知
- Mutex
互斥体
- Process
处理
- Semaphore
信号
- Thread
线
- Waitable timer
等待计时器
So, if you want to wait on a process until it ends, you can wait on the process's handle, which is accessible via the Process.Handle
property. 因此,如果您要等待某个过程直到结束,则可以等待该过程的句柄,可通过
Process.Handle
属性访问该句柄。
But you don't actually need to P/Invoke the Win32 function at all. 但是实际上您根本不需要P /调用Win32函数。 The .NET
Process
wrapper class has WaitForExit()
and WaitForInputIdle()
member functions that can be used to wait on a process (note that both have overloads taking a timeout value). .NET
Process
包装器类具有WaitForExit()
和WaitForInputIdle()
成员函数,可用于在进程上等待(请注意,这两个函数都具有采用超时值的重载)。
If this is a process that you started using the Process
class wrapper, you can simply interrogate the Process.HasExited
property. 如果这是您开始使用
Process
类包装器启动的Process
,则可以简单地询问Process.HasExited
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.