在delphi中,如何判断一个进程运行结束!(100分)

  • 主题发起人 主题发起人 honglinxiong
  • 开始时间 开始时间
H

honglinxiong

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在帮别人做一个小程序,它的功能是运行完一个安装程序后继续运行下一个安装程序,所以碰到了这个问题!
第一个安装程序我用CreateProcess调用,然后我想在第一个进程运行完之后继续用另外一个进程调用第二个安装程序!
请各位帮忙!
 
findwindow
GetProcessTimes
都可以做到
 
给你一段代码,既然你已经使用了createprocess就没有什么问题了。
用一个api就搞定你得问题。不清楚得话,可以看帮助或继续问
var
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
fillchar(startupinfo,sizeof(TstartupInfo),0);
fillchar(ProcessInfo,sizeof(TProcessInformation),0);
if not CreateProcess(nil,
pchar('notepad.exe'), { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
normal_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then
showmessage('false') else
showmessage('success');
waitforsingleobject(ProcessInfo.hProcess,INFINITE);{或者设为毫秒数,可以超时等待。}
showmessage('可以了');
 
另外,给你转发一个帖子,更上面得一样得。呵呵
Howdo
I execute a program and have my code wait until it is finished?
From: Noel Rice <nrice@ix.netcom.com>
A: Here is the 16 bit version:

--------------------------------------------------------------------------------
uses Wintypes,WinProcs,Toolhelp,Classes,Forms;
Function WinExecAndWait(Path : string;
Visibility : word) : word;
var
InstanceID : THandle;
PathLen : integer;
begin
{ inplace conversion of a String to a PChar }
PathLen := Length(Path);
Move(Path[1],Path[0],PathLen);
Path[PathLen] := #00;
{ Try to run the application }
InstanceID := WinExec(@Path,Visibility);
if InstanceID < 32 then
{ a value less than 32 indicates an Exec error }
WinExecAndWait := InstanceID
else
begin
Repeat
Application.ProcessMessages;
until Application.Terminated or (GetModuleUsage(InstanceID) = 0);
WinExecAndWait := 32;
end;
end;

--------------------------------------------------------------------------------
Here is the 32 bit version:

--------------------------------------------------------------------------------
function WinExecAndWait32(FileName:String;
Visibility : integer):integer;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then
Result := -1 { pointer to PROCESS_INF }
else
begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;


 
参照shangshang给我的源代码,我试了一下
但不知什么原因,我的第一个安装程序没有执行完,showmessage('可以了');就执行了
我非常希望在setup.exe执行完之后再执行showmessage('可以了');
不知可不可以??
 
各位大侠行行好吧,小弟我都快急死了!
 
我的代码的本意就是那样啊,你可以试一下 我的代码。
打开一个记事本试试,肯定是关闭记事本后才能执行showmessage;
但是你的问题毛病出在哪里,我实在不清楚了。
能把代码贴一下吗?
 
procedure TForm1.Button1Click(Sender: TObject);
var
proc_info: TProcessInformation;
startinfo: TStartupInfo;
//-----------------------------------------
proc_info1: TProcessInformation;
startinfo1: TStartupInfo;
begin
FillChar(proc_info, sizeof(TProcessInformation), 0);
FillChar(startinfo, sizeof(TStartupInfo), 0);
startinfo.cb := sizeof(TStartupInfo);
//----------------------------------------
FillChar(proc_info1, sizeof(TProcessInformation), 0);
FillChar(startinfo1, sizeof(TStartupInfo), 0);
startinfo1.cb := sizeof(TStartupInfo);
//------------------------------------------
if CreateProcess(nil,'f:/考务系统/msde/setup.exe', nil,nil, false, NORMAL_PRIORITY_CLASS, nil, nil,startinfo, proc_info) then
begin
WaitForSingleObject(proc_info.hProcess, INFINITE);
CloseHandle(proc_info.hProcess);
CloseHandle(proc_info.hThread);
CreateProcess(nil,'f:/考务系统/区县/setup.exe', nil,nil, false, NORMAL_PRIORITY_CLASS, nil, nil,startinfo1, proc_info1);
end
else
begin
Application.MessageBox('不能执行程序! '+ 'application', 'Error', MB_ICONEXCLAMATION);
end;
end;
 
你要连续等待两个PROCESS啊,早说啊,方法是一样的了。没问题。
procedure TForm1.Button1Click(Sender: TObject);
var
proc_info: TProcessInformation;
startinfo: TStartupInfo;
//-----------------------------------------
proc_info1: TProcessInformation;
startinfo1: TStartupInfo;
begin
FillChar(proc_info, sizeof(TProcessInformation), 0);
FillChar(startinfo, sizeof(TStartupInfo), 0);
startinfo.cb := sizeof(TStartupInfo);
//----------------------------------------
FillChar(proc_info1, sizeof(TProcessInformation), 0);
FillChar(startinfo1, sizeof(TStartupInfo), 0);
startinfo1.cb := sizeof(TStartupInfo);
//------------------------------------------
if CreateProcess(nil, 'c:/setup.exe', nil, nil, false, NORMAL_PRIORITY_CLASS, nil, nil, startinfo, proc_info) then
begin
WaitForSingleObject(proc_info.hProcess, INFINITE);
CloseHandle(proc_info.hProcess);
CloseHandle(proc_info.hThread);
//----------------------------------------
FillChar(proc_info1, sizeof(TProcessInformation), 0);
FillChar(startinfo1, sizeof(TStartupInfo), 0);
startinfo1.cb := sizeof(TStartupInfo);
//------------------------------------------
if CreateProcess(nil, 'c:/setup.exe', nil, nil, false, NORMAL_PRIORITY_CLASS, nil, nil, startinfo1, proc_info1) then
begin
WaitForSingleObject(proc_info.hProcess, INFINITE);
CloseHandle(proc_info.hProcess);
CloseHandle(proc_info.hThread);
end;

showmessage('可以了。呵呵');
end
else
begin
Application.MessageBox('不能执行程序! ' + 'application', 'Error', MB_ICONEXCLAMATION);
end;

end;
 
多人接受答案了。
 
后退
顶部