WaitForSingleObject return ERROR_INVALID_HANDLE
WaitForSingleObject return ERROR_INVALID_HANDLE
我试图通过调用 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
UInt32 r = WaitForSingleObject(handle, 0);
if(r == WAIT_OBJECT_0)
{
MessageBox.Show("still running!");
}
if(r == WAIT_FAILED)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
您不能等待 window 句柄。您可以将 window 句柄传递给 window 相关函数,例如 IsIconic()
,但它们不是内核对象,因此您不能等待它们。 The documentation 给出了您可以等待的对象列表:
The WaitForSingleObject function can wait for the following objects:
- Change notification
- Console input
- Event
- Memory resource notification
- Mutex
- Process
- Semaphore
- Thread
- Waitable timer
所以,如果你想等待一个进程直到它结束,你可以等待进程的句柄,可以通过 Process.Handle
属性.
但实际上您根本不需要 P/Invoke Win32 函数。 .NET Process
包装器 class 具有 WaitForExit()
and WaitForInputIdle()
可用于等待进程的成员函数(请注意,两者都有采用超时值的重载)。
如果这是您使用 Process
class 包装器启动的进程,您可以简单地询问 Process.HasExited
属性.
我试图通过调用 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
UInt32 r = WaitForSingleObject(handle, 0);
if(r == WAIT_OBJECT_0)
{
MessageBox.Show("still running!");
}
if(r == WAIT_FAILED)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
您不能等待 window 句柄。您可以将 window 句柄传递给 window 相关函数,例如 IsIconic()
,但它们不是内核对象,因此您不能等待它们。 The documentation 给出了您可以等待的对象列表:
The WaitForSingleObject function can wait for the following objects:
- Change notification
- Console input
- Event
- Memory resource notification
- Mutex
- Process
- Semaphore
- Thread
- Waitable timer
所以,如果你想等待一个进程直到它结束,你可以等待进程的句柄,可以通过 Process.Handle
属性.
但实际上您根本不需要 P/Invoke Win32 函数。 .NET Process
包装器 class 具有 WaitForExit()
and WaitForInputIdle()
可用于等待进程的成员函数(请注意,两者都有采用超时值的重载)。
如果这是您使用 Process
class 包装器启动的进程,您可以简单地询问 Process.HasExited
属性.