如何判断当一个程序结束后,立即开始执行另一个程序,不要他们同时运行!(30分)

  • 主题发起人 主题发起人 natolee
  • 开始时间 开始时间
N

natolee

Unregistered / Unconfirmed
GUEST, unregistred user!
我想先运行rar.exe,当它压缩完成后,开始删除它压缩的文件夹,可是它还没有压缩完,<br>程序已经开始删除了,结果无法删除文件夹!
 
查找进程判断
 
首先CreatrProcess创建一个进程,然后用WaitForSingleobject 等待这个进程的结束。
 
长见识了
 
你用的是哪个版本的RAR.EXE,如果是最新的Winrar(3.2)安装目录下的RAR.EXE,只要加个参数-df就行了!不过这个的RAR.EXE只能在Windows平台下使用,2.5以前的版本没有这个参数。
 
Function WinExecAndWait32V2( FileName: String; Visibility: integer ): DWORD;<br>&nbsp; Procedure WaitFor( processHandle: THandle );<br>&nbsp; &nbsp; Var<br>&nbsp; &nbsp; &nbsp; msg: TMsg;<br>&nbsp; &nbsp; &nbsp; ret: DWORD;<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; ret := MsgWaitForMultipleObjects(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1, { 1 handle to wait on }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;processHandle, { the handle }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;False, { wake on any event }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INFINITE, { wait without timeout }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QS_PAINT or { wake on paint messages }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;QS_SENDMESSAGE { or messages from other threads }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;);<br>&nbsp; &nbsp; &nbsp; &nbsp; If ret = WAIT_FAILED Then Exit; { can do little here }<br>&nbsp; &nbsp; &nbsp; &nbsp; If ret = (WAIT_OBJECT_0 + 1) Then Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { Woke on a message, process paint messages only. Calling<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PeekMessage gets messages send from other threads processed. }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DispatchMessage( msg );<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; Until ret = WAIT_OBJECT_0;<br>&nbsp; &nbsp; End; { Waitfor }<br>Var { V1 by Pat Ritchey, V2 by P.Below }<br>&nbsp; zAppName:array[0..512] of char;<br>&nbsp; StartupInfo:TStartupInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>Begin { WinExecAndWait32V2 }<br>&nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := Visibility;<br>&nbsp; If not CreateProcess(nil,<br>&nbsp; &nbsp; zAppName, { pointer to command line string }<br>&nbsp; &nbsp; nil, { pointer to process security attributes }<br>&nbsp; &nbsp; nil, { pointer to thread security attributes }<br>&nbsp; &nbsp; false, { handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or { creation flags }<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil, { pointer to new environment block }<br>&nbsp; &nbsp; nil, { pointer to current directory name }<br>&nbsp; &nbsp; StartupInfo, { pointer to STARTUPINFO }<br>&nbsp; &nbsp; ProcessInfo) { pointer to PROCESS_INF }<br>&nbsp; Then<br>&nbsp; &nbsp; Result := DWORD(-1) { failed, GetLastError has error code }<br>&nbsp; Else Begin<br>&nbsp; &nbsp; &nbsp;Waitfor(ProcessInfo.hProcess);<br>&nbsp; &nbsp; &nbsp;GetExitCodeProcess(ProcessInfo.hProcess, Result);<br>&nbsp; &nbsp; &nbsp;CloseHandle( ProcessInfo.hProcess );<br>&nbsp; &nbsp; &nbsp;CloseHandle( ProcessInfo.hThread );<br>&nbsp; End; { Else }<br>End; { WinExecAndWait32V2 }
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部