现在的位置: 首页 > 综合 > 正文

WaitForInputIdle 的注意细节

2012年10月10日 ⁄ 综合 ⁄ 共 1814字 ⁄ 字号 评论关闭

  MSDN上如此说:
  The WaitForInputIdle function waits until the specified process is waiting for user input with no input pending, or until the time-out interval has elapsed.
  DWORD WaitForInputIdle( HANDLE hProcess, // handle to process
         DWORD dwMilliseconds // time-out interval
      );

  Parameters
hProcess
  
[in] Handle to the process. If this process is a console application or does not have a message queue, WaitForInputIdle returns immediately.
dwMilliseconds
  
[in] Specifies the time-out interval, in milliseconds. If dwMilliseconds is INFINITE, the function does not return until the process is idle.

  Return Values
  The following table shows the possible return values:

Value Meaning
0 The wait was satisfied successfully.
WAIT_TIMEOUT The wait was terminated because the time-out interval elapsed.
-1 An error occurred. To get extended error information, use the GetLastError function.

  Remarks

  The WaitForInputIdle function enables a thread to suspend its execution until the specified process has finished its initialization and is waiting for user input with no input pending. This can be useful for synchronizing a parent process and a newly created child process. When a parent process creates a child process, the CreateProcess function returns without waiting for the child process to finish its initialization. Before trying to communicate with the child process, the parent process can use WaitForInputIdle to determine when the child's initialization has been completed. For example, the parent process should use WaitForInputIdle before trying to find a window associated with the child process.

The WaitForInputIdle function can be used at any time, not just during application startup.

Requirements

Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.

 

  补充:我觉得WaitForInputIdle这个函数在CreateProcess之后只是为新进程初始化进程代码然后就返回,并不是等进程完全创建好在返回。这一点大家可以用有界面的程序来测试,因为有界面的程序在代码初始化完成之后还要做一些其他事情然后再显示出界面的,所以创建有界面的程序显示界面是WaitForInputIdle已经返回了。如有不对,请指教。

抱歉!评论已关闭.