等待一个进程结束,再启动另外一个进程? (50分)

  • 主题发起人 主题发起人 littlefish
  • 开始时间 开始时间
L

littlefish

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; CreateProcessResult := CreateProcess(nil,<br>&nbsp; &nbsp; &nbsp;zAppName,<br>&nbsp; &nbsp; &nbsp;nil,<br>&nbsp; &nbsp; &nbsp;nil,<br>&nbsp; &nbsp; &nbsp;true,<br>&nbsp; &nbsp; &nbsp;CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; &nbsp;nil,<br>&nbsp; &nbsp; &nbsp;nil,<br>&nbsp; &nbsp; &nbsp;StartupInfo,<br>&nbsp; &nbsp; &nbsp;ProcessInfo);<br><br>&nbsp; if not CreateProcessResult then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showMessage('失败');<br>&nbsp; &nbsp; &nbsp; Form1.Close;<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess, INFINITE);<br>&nbsp; &nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess, FAppState);<br>&nbsp; &nbsp; end;<br>&nbsp; <br>&nbsp; CreateProcess(...); //开始另外一个进程。<br>end;<br>如题所说,我用WaitforSingleObject等待进程结束再去启动另外一个进程。实现是没问题<br>可是用WaitforSingleObject等待第一个进程结束的过程中,Form1是无法响应任何消息的。<br>最简单的现象,当我拖动Form1前面的一个窗口(刚打开的进程的窗口)时Form1不会刷新。<br>有什么别的方法解决这个问题?
 
放到线程中去。
 
不知道用线程调用一个外部程序和用进程调用从功能上有什么不同吗?我调用的外部程序<br>本身就是多线程的。
 
1 用线程做<br>2 设置临界区<br>&nbsp; PUBLIC<br>&nbsp; &nbsp;VAR<br>&nbsp; &nbsp; &nbsp;CS:TRTLCRITICALSECTION;<br>&nbsp; InitializeCriticalSection(CS); //<br>&nbsp;//在FORM 的CREATE 中完成<br><br>&nbsp;在线程执行中:<br>&nbsp; enterCriticalSection(cs) // enter<br>&nbsp; ......<br>&nbsp; &nbsp;{ your code<br>&nbsp; &nbsp; }<br>&nbsp; leaveCriticalSection(cs) // leave<br><br>&nbsp;在FORM close <br>&nbsp; DeleteCriticalSection(cs)<br>3:说明:<br>&nbsp; 该方法是将线程中保护公共变量的方法引申到保护整个线程的执行。<br>4。还可用信号量的方法做,道理一样。<br><br>&nbsp;<br>&nbsp;
 
多人接受答案了。
 
后退
顶部