delphi – 启动进程并侦听退出事件
发布时间:2020-12-15 09:26:50 所属栏目:大数据 来源:网络整理
导读:我有一些代码启动一个进程并挂钩一个事件处理程序来处理进程退出时,我的代码是用C#编写的,我想知道Delphi是否可以实现类似的东西. System.Diagnostics.Process myProcess = new System.Diagnostics.Process();myProcess.StartInfo.FileName = "notepad.exe";
我有一些代码启动一个进程并挂钩一个事件处理程序来处理进程退出时,我的代码是用C#编写的,我想知道Delphi是否可以实现类似的东西.
System.Diagnostics.Process myProcess = new System.Diagnostics.Process(); myProcess.StartInfo.FileName = "notepad.exe"; myProcess.EnableRaisingEvents = true; myProcess.Exited += new System.EventHandler(Process_OnExit); myProcess.Start(); public void Process_OnExit(object sender,EventArgs e) { //Do something when the process ends } 我对Delphi了解不多,所以任何帮助都会受到赞赏,谢谢. 解决方法
是的,你可以用Delphi做类似的事情.我还没有看到使用事件处理程序,但您可以创建一个进程,等待它完成,然后在发生这种情况时执行某些操作.如果你想在同一时间做某事,把它放在另一个线程中.
这里有一些代码用于创建一个进程并等待我从网上删除: procedure ExecNewProcess(ProgramName : String; Wait: Boolean); var StartInfo : TStartupInfo; ProcInfo : TProcessInformation; CreateOK : Boolean; begin { fill with known state } FillChar(StartInfo,SizeOf(TStartupInfo),0); FillChar(ProcInfo,SizeOf(TProcessInformation),0); StartInfo.cb := SizeOf(TStartupInfo); CreateOK := CreateProcess(nil,PChar(ProgramName),nil,False,CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS,StartInfo,ProcInfo); { check to see if successful } if CreateOK then begin //may or may not be needed. Usually wait for child processes if Wait then WaitForSingleObject(ProcInfo.hProcess,INFINITE); end else begin //ShowMessage('Unable to run '+ProgramName); SysErrorMessage(GetLastError()); end; CloseHandle(ProcInfo.hProcess); CloseHandle(ProcInfo.hThread); end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |