用WinExec执行A程序
用WaitForSingleObject等待A的结束
用WinExec执行B程序
用WaitForSingleObject等待B的结束
用WinExec执行B程序
用WaitForSingleObject等待B的结束
...
用WaitForSingleObject等待A的结束
用WinExec执行B程序
用WaitForSingleObject等待B的结束
用WinExec执行B程序
用WaitForSingleObject等待B的结束
...
调试欢乐多
用WinExec运行程序不能得到该程序的过程句柄,必须改用ShellExecute(或ShellExecuteEx、CreateProcess、CreateProcessAsUser)运行,用得到的进程句柄作为参数调用WaitForSingleobject.
下面是用ShellExecuteEx实现的(Uses ShellAPI):
procedure RunAndWait(Directory,FileName,Param:String;hwndParent:HWND);
var
ExecInfo:TShellExecuteInfo;
begin
ExecInfo.cbSize := sizeof(ExecInfo);
ExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS ;
ExecInfo.lpVerb := nil;
ExecInfo.lpFile := PChar(FileName);
ExecInfo.lpParameters := PChar(Param);
ExecInfo.lpDirectory := PChar(Directory);
ExecInfo.nShow := SW_NORMAL;
ExecInfo.Wnd := hwndParent;
ShellExecuteEx(@ExecInfo);
if (ExecInfo.hProcess <> 0) then
WaitForSingleObject(ExecInfo.hProcess,INFINITE);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RunAndWait('','calc.exe','',Handle);
RunAndWait('','notepad.exe','',Handle);
RunAndWait('','mspaint.exe','',Handle);
end;