unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, IdGlobal; type TForm1 = class(TForm) btnStart: TButton; Memo1: TMemo; Timer1: TTimer; procedure btnStartClick(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; piProcInfo: PROCESS_INFORMATION; const AppName = 'notepad.exe'; implementation {$R *.dfm} function EstablishProcess: Boolean; var siStartup: STARTUPINFO; saProcess, saThread: SECURITY_ATTRIBUTES; begin ZeroMemory(@siStartup, sizeof(siStartup)); siStartup.cb := sizeof(siStartup); saProcess.nLength := sizeof(saProcess); saProcess.lpSecurityDescriptor := PChar(nil); saProcess.bInheritHandle := true; saThread.nLength := sizeof(saThread); saThread.lpSecurityDescriptor := PChar(nil); saThread.bInheritHandle := true; Result := CreateProcess(nil, PChar(AppName),@saProcess,@saThread,false, CREATE_DEFAULT_ERROR_MODE,Pchar(nil),Pchar(nil),siStartup,piProcInfo); end; function CheckAndRestart: Boolean; var dwExitCode: DWORD; fExit: Boolean; begin Result := true; dwExitCode := 0; fExit := false; fExit := GetExitCodeProcess(piProcInfo.hProcess, dwExitCode); if (fExit and (dwExitCode <> STILL_ACTIVE)) then begin CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); Result := EstablishProcess; end; end; procedure TForm1.btnStartClick(Sender: TObject); begin Memo1.Lines.Add( DateTimeToStr(Now) + ': ' + 'Run ' + AppName + ' ' + iif(EstablishProcess, 'Successfully', 'UnSuccessfully')); btnStart.Enabled := false; end; procedure TForm1.Timer1Timer(Sender: TObject); begin Memo1.Lines.Add( DateTimeToStr(Now) + ': ' + 'Restart ' + AppName + ' ' + iif(CheckAndRestart, 'Successfully ( or Still Alive)', 'UnSuccessfully')); end; end.
[Code Collection] 定时启动notepad.exe
最新推荐文章于 2019-09-16 17:03:22 发布