📄 0516001.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>
<body>
<div align="justify">
<table border="0" width="100%" class="font" height="57">
<tr>
<td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">VC知识库(五)</font>
</td>
<td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">www.vckbase.com</font>
</td>
</tr>
<tr>
<td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
<big><strong>启动和等待进程结束</strong></big>
</td>
</tr>
<tr>
<td width="100%" height="17" class="info" align="center" colspan="2">
<strong>闻怡洋</strong>
</td>
</tr>
<tr>
<td width="100%" height="22" class="font" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="5" class="font" colspan="2">
<p>我们在开发是经常需要在启动一个进程之后等待其结束后再继续运行。</p>
<p>在这里提供了一个名为Wait的函数,它会为你完成上面的功能。实现的思想是在启动进程后等待其结束,由于进程是一种资源,而资源的句柄在WIN32中可以作为核心量使用。你可以使用WaitForSingleObject等待核心量状态改变为有信号状态。对进程来讲当进程结束时其状态转变为有信号。</p>
<p>在本例中使用了一个单独的线程来启动进程并等待结束。</p>
<h4>Wait()</h4>
<p><font color="#804040"><small>BOOL Wait(CString szCmdLine)<br>
{<br>
LPTHREADINFO pThreadInfo = new THREADINFO;<br>
CEvent *pThreadEvent = new CEvent(FALSE, TRUE); <br>
ASSERT_VALID(pThreadEvent);<br>
if(pThreadInfo)<br>
{<br>
pThreadInfo->pTermThreadEvent =
pThreadEvent;<br>
pThreadInfo->strPathName = szCmdLine;<br>
<br>
AfxBeginThread(LaunchAndWait, pThreadInfo);<br>
WaitForSingleObject(pThreadEvent->m_hObject,
INFINITE);<br>
return TRUE;<br>
}<br>
return FALSE;<br>
}</small></font></p>
<h4>LaunchAndWait()</h4>
<p><font color="#804040"><small>UINT LaunchAndWait(LPVOID pParam)<br>
{<br>
LPTHREADINFO pThreadInfo = (LPTHREADINFO) pParam;<br>
PROCESS_INFORMATION stProcessInfo;<br>
if(LaunchApplication(pThreadInfo->strPathName, &stProcessInfo))<br>
{<br>
HANDLE hThreads[2];<br>
hThreads[0] =
pThreadInfo->pTermThreadEvent->m_hObject;<br>
hThreads[1] = stProcessInfo.hProcess;<br>
DWORD dwIndex = WaitForMultipleObjects(2,
hThreads, FALSE, INFINITE);<br>
CloseHandle(stProcessInfo.hThread);<br>
CloseHandle(stProcessInfo.hProcess);<br>
pThreadInfo->pTermThreadEvent->SetEvent();<br>
if(pThreadInfo)<br>
delete pThreadInfo;<br>
}<br>
else<br>
pThreadInfo->pTermThreadEvent->SetEvent();<br>
return 0;<br>
}<br>
</small></font></p>
<h4><font color="#000000"><big>LaunchApplication()</big></font></h4>
<p><font color="#804040"><small>BOOL LaunchApplication(LPCTSTR pCmdLine,
PROCESS_INFORMATION *pProcessInfo)<br>
{<br>
STARTUPINFO stStartUpInfo;<br>
<br>
memset(&stStartUpInfo, 0, sizeof(STARTUPINFO));<br>
<br>
stStartUpInfo.cb = sizeof(STARTUPINFO);<br>
<br>
stStartUpInfo.dwFlags = STARTF_USESHOWWINDOW;<br>
stStartUpInfo.wShowWindow = SW_SHOWDEFAULT;<br>
<br>
return CreateProcess(NULL, (LPTSTR)pCmdLine, NULL, NULL, FALSE,<br>
NORMAL_PRIORITY_CLASS, NULL, <br>
NULL, &stStartUpInfo, pProcessInfo);<br>
}</small></font></p>
</td>
</tr>
<tr>
<td width="100%" height="12" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="6" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="8" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="17" class="font" colspan="2"></td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -