请教,我想写一个守护进程 ,用来监控我的服务端程序,如果服务端程序退出就重新启动,应该怎么写

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      hd: Thandle;
    begin
      hd := findwindow(nil, '计算器');
      if hd=0 then
      begin
        winexec(PChar('calc'), SW_SHOW);
      end;
    end;这样算不算代码呢?
      

  2.   

    不难写的
    监控进程嘛
    用PsAPI即可
    发现关闭了再CreateProcess
      

  3.   

    unit UMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls, RpMemo, ComCtrls;const intMax = 255; //设置最大控制进程数type
      TForm1 = class(TForm)
        Button1: TButton;
        Timer1: TTimer;
        StatusBar1: TStatusBar;
        memo1: TMemo;
        Button2: TButton;
        function funEstablishProcess(strFileName: string): boolean; //为程序创建子进程
        function funInitFileStatus(): boolean; //初始化进程的相关信息
        function funGetCurrentDir(const str: string): string; //从字符串取得该程序路径
        function funAppIsRunning(const str: string): boolean; //判断程序是否已在运行
        procedure Button1Click(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;type intFileNameStruct = record //保存进程相关信息
        intOrder: integer;
        strFileName: string;
        strExeName: string;
      end;var
      Form1: TForm1;
      piProcInfoGPS: array[1..intMax] of PROCESS_INFORMATION;
      intFileStatus: array[1..intMax] of intFileNameStruct;
      intNowCount: integer; //目前列表中进程数量
    implementation{$R *.dfm}{ TForm1 }function TForm1.funEstablishProcess(strFileName: string): boolean;
    var
      siStartupInfo: STARTUPINFO;
      saProcess, saThread: SECURITY_ATTRIBUTES;
      fSuccess: boolean;
      i: integer;
    begin
      fSuccess := false;
      i := 1;  ZeroMemory(@siStartupInfo, sizeof(siStartupInfo));
      siStartupInfo.cb := sizeof(siStartupInfo);
      saProcess.nLength := sizeof(saProcess);
      saProcess.lpSecurityDescriptor := PChar(nil);
      saProcess.bInheritHandle := true;
      saThread.nLength := sizeof(saThread);
      saThread.lpSecurityDescriptor := PChar(nil);
      saThread.bInheritHandle := true;
      while (i <= intNowCount) do
      begin
        if (intFileStatus[i].strFileName = strFileName) then
          break;
        i := i + 1;
      end;
      fSuccess := CreateProcess(PChar(nil), pChar(strFileName), @saProcess, @saThread, false,
        CREATE_DEFAULT_ERROR_MODE, Pchar(nil), Pchar(funGetCurrentDir(intFileStatus[i].strFileName)), siStartupInfo, piProcInfoGPS[i]);
      if (not fSuccess) then
      begin
        intFileStatus[i].intOrder := 0;
        Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' fail.');
      end
      else
      begin
        intFileStatus[i].intOrder := 1;
        Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' success.');
      end;
      Result := true;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var i: integer;
    begin
      i := 0;  for i := 1 to intNowCount do
      begin
        if (funAppIsRunning(intFileStatus[i].strExeName) = False) then
          funEstablishProcess(intFileStatus[i].strFileName);
      end;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    var
      dwExitCode: DWORD;
      fprocessExit: boolean;
      i: integer;
    begin
      dwExitCode := 0;
      fprocessExit := false;
      i := 0;  for i := 1 to intNowCount do
      begin
        fprocessExit := GetExitCodeProcess(piProcInfoGPS[i].hProcess, dwExitCode);
        if (fprocessExit and (dwExitCode <> STILL_ACTIVE)) then
        begin
          Memo1.Lines.Add('进程终止');
          CloseHandle(piProcInfoGPS[i].hThread);
          CloseHandle(piProcInfoGPS[i].hProcess);
          intFileStatus[i].intOrder := 0;
          funEstablishProcess(intFileStatus[i].strFileName);
        end;
      end;  statusbar1.Panels[0].Text := '进程管理';
      statusbar1.Panels[1].Text := '进程数量:' + inttostr(intNowCount) + '个';
      statusbar1.Panels[2].Text := datetimetostr(now);
    end;
    function TForm1.funInitFileStatus: boolean;
    var i: integer;
      strFileNameList: TstringList;
    begin
      i := 0;  strFileNameList := TstringList.Create();
      strFileNameList.LoadFromFile('FileNameList.txt');
      for i := 1 to strFileNameList.Count do
      begin
        intFileStatus[i].intOrder := 0;
        intFileStatus[i].strFileName := strFileNameList.Strings[i - 1];
        intFileStatus[i].strExeName :=
          copy(intFileStatus[i].strFileName,
          length(funGetCurrentDir(intFileStatus[i].strFileName)) + 2, //2 is len('\')+1
          length(intFileStatus[i].strFileName)-length(funGetCurrentDir(intFileStatus[i].strFileName))-5);//5 is len('.exe')+1
      end;  intNowCount := strFileNameList.Count;
      strFileNameList.Free;
      Result := true;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      funInitFileStatus;
    end;function TForm1.funGetCurrentDir(const str: string): string;
    var i: integer;
    begin
      for i := length(str)-1 downto 0 do
      begin
        if str[i] = '\' then
          break;
      end;
      Result := copy(str, 0, i - 1);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Form1.Close;
    end;function TForm1.funAppIsRunning(const str: string): boolean;
    var intFileHwnd: hWnd;
    begin
      Result := False;  intFileHwnd := windows.FindWindow(nil, Pchar(str));
      if intFileHwnd <> 0 then
        Result := true;
    end;end.
      

  4.   

    我需要启动的所有进程写在:FileNameList.txt中
    在这个文本中的进程,如果被结束后,程序会在4秒钟把该进程启动,轮询时间你自己设置好了。
      

  5.   

    你建文本后,需要执行的路径这样写(不要换行,我是按照一行做为一个进程处理的):
    D:\work\CountCash\CountCash.exe
    D:\work\calculate\Calculate.exe
      

  6.   

    写一个服务吧,或是用下面的代码,
    while(1)
    {
    hProcess=CreateProcess(  );
    WaitForSingle(hProcess);
    }
    当你创建的进程退出后,等待结束,就会重新创建进程,这样就不用不停的去检测你的进程到底有没有被关闭
      

  7.   

    if (fprocessExit and (dwExitCode <> STILL_ACTIVE)) then
    这个处理,包含了进程的无响应,这时先结束进程,再重启