父进程 等待子进程初始化后才使用子进程_waitforinputidle
2012-11-12 18:45
621 查看
在进程中创建子进程是很常见的话题。常规的方法是用CreateProcess(),这个函数功能强大,使用起来也很方便。不过CreateProcess()或其他函数,如ShellExecuteEx(),在创建子进程后,并不等待子进程初始化完毕,而是立即返回。
通常你应该等子进程初始化完毕后再开始其它事情,特别是子进程有消息循环时。这可以通过函数WaitForInputIdle()实现。这个函数要求你提供子进程的句柄,CreateProcess()和ShellExecuteEx()都返回子进程的句柄。
WaitForInputIdle()优于sleep()。例如,sleep(3000)一定使进程睡3s。有时候多了,有时候少了。当sleep()结束时,子进程可能初始化完毕,也可能没有,但父进程都立即恢复执行。使用ForInputIdle(),当子进程初始化完毕后,父进程才继续执行。
ShellExecuteEx()也可以启动进程。不过ShellExecuteEx()常用在这样的场合,象打开pdf或txt等。即你不知道应用程序的名字,只知道文件名的时候。
// Creates a child process and waits until the child process finishes initialization
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR szCommandLine[] = _T(//"notepad.exe//");
BOOL bSuccess = CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (bSuccess)
{
::WaitForInputIdle (pi.hProcess, 5000L);
// close the handle if no longer used
::CloseHandle(pi.hProcess);
// do whatever you want to do
//HWND hBBBWnd = ::FindWindow(NULL, strBBBWindowName);
// ... ...
}
简单的讲就是: 父进程创建子进程后 要先确定子进程初始化好了后才能使用子进程 。那么有什么方法可以让父进程知道子进程已经准备好了呢 》?那就是父进程调用waitForInputIdle( 子进程id, 超时),如果子进程没初始化好 。那么父进程将悬挂。知道 超时或子进程准备好。
一下内容摘自msdn:
The WaitForInputIdle function only works with GUI applications. If a console application calls the function, it returns immediately, with no wait.
and a newly created child process. When a parent process creates a child process, theCreateProcess 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 WaitForInputIdlebefore trying to find a window associated with the child process.
The WaitForInputIdle function can be used at any time, not just during application startup.
通常你应该等子进程初始化完毕后再开始其它事情,特别是子进程有消息循环时。这可以通过函数WaitForInputIdle()实现。这个函数要求你提供子进程的句柄,CreateProcess()和ShellExecuteEx()都返回子进程的句柄。
WaitForInputIdle()优于sleep()。例如,sleep(3000)一定使进程睡3s。有时候多了,有时候少了。当sleep()结束时,子进程可能初始化完毕,也可能没有,但父进程都立即恢复执行。使用ForInputIdle(),当子进程初始化完毕后,父进程才继续执行。
ShellExecuteEx()也可以启动进程。不过ShellExecuteEx()常用在这样的场合,象打开pdf或txt等。即你不知道应用程序的名字,只知道文件名的时候。
// Creates a child process and waits until the child process finishes initialization
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR szCommandLine[] = _T(//"notepad.exe//");
BOOL bSuccess = CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (bSuccess)
{
::WaitForInputIdle (pi.hProcess, 5000L);
// close the handle if no longer used
::CloseHandle(pi.hProcess);
// do whatever you want to do
//HWND hBBBWnd = ::FindWindow(NULL, strBBBWindowName);
// ... ...
}
简单的讲就是: 父进程创建子进程后 要先确定子进程初始化好了后才能使用子进程 。那么有什么方法可以让父进程知道子进程已经准备好了呢 》?那就是父进程调用waitForInputIdle( 子进程id, 超时),如果子进程没初始化好 。那么父进程将悬挂。知道 超时或子进程准备好。
一下内容摘自msdn:
WaitForInputIdle
The WaitForInputIdle function waits until the given process is waiting for user input with no input pending, or until the time-out interval has elapsed.The WaitForInputIdle function only works with GUI applications. If a console application calls the function, it returns immediately, with no wait.
DWORD WaitForInputIdle( HANDLE hProcess, // handle to process DWORD dwMilliseconds // time-out interval in milliseconds );
Parameters
hProcessHandle to the process.dwMillisecondsSpecifies 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. |
0xFFFFFFFF | An error occurred. To get extended error information, use the GetLastErrorfunction. |
Remarks
The WaitForInputIdle function enables a thread to suspend its execution until a specified process has finished its initialization and is waiting for user input with no input pending. This can be useful for synchronizing a parent processand a newly created child process. When a parent process creates a child process, theCreateProcess 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 WaitForInputIdlebefore trying to find a window associated with the child process.
The WaitForInputIdle function can be used at any time, not just during application startup.
相关文章推荐
- 父进程 等待子进程初始化后才使用子进程_waitforinputidle
- WaitForInputIdle简介
- 子进程等待父进程退出及fork使用
- 验证子进程退出时会给父进程发送信号的机制 、 编写父进程等待子进程的异步版本
- WaitForInputIdle
- 执行命令行并等待完成(使用WaitForSingleObject达到目的)
- WaitForInputIdle 的注意细节
- 改进的延时函数Delay(使用MsgWaitForMultipleObjects等待消息或超时的到来)
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 等待进程终止WaitForSingleObject()
- 父进程等待子进程终止 wait, WIFEXITED, WEXITSTATUS
- 进程等待-wait_event_interruptible 使用方法
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- shell之使用eval模拟一维数组保存进程pids,并调用wait等待所有进程执行完返回
- linux下的wait()实现等待一个子进程和所有子进程退出
- delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- 主线程使用WaitForSingleObject和MsgWaitForMultipleObjects等待线程返回值