例如工程为prj1
怎样在运行prj1时检查到该程序已经运行了?

解决方案 »

  1.   

    CreateMutex

    CreateMapFile()
      

  2.   

    program Project1;uses
      Forms,windows,          //use windows
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin  CreateMutex(nil, True, 'MyAppClass');
      if GetLastError = ERROR_ALREADY_EXISTS then
      begin
        MessageBox(0, '程序已经运行', '88!' ,MB_ICONERROR);
        Halt;
      end;  Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
      

  3.   

    var
    wnd:hwnd;
    begin
    wnd:=findwindow(nil,'ZW123');
    IF wnd <>0 THEN
    begin
    showmessage('程序已运行!,只能运行一次!');
    case isiconic(wnd) of
    true:showwindow(wnd,sw_restore);
    false:begin showwindow(wnd,SW_showNORMAL);setforegroundwindow(wnd);end;
    end;
    application.Terminate;
    end;
    FORM1.Caption:='ZW123';
    end;
      

  4.   

    //项目文件...beginApplication.initialation;if CreateMutex then beginApplication.CreateForm(Form1, TForm1);Application.Run;end elseDestroyMutex;end;//主窗体文件...implementationvarMutex: hWnd;function CreateMutex: Boolean;varPrevInstHandle: THandle;AppTitle: PChar;beginAppTitle := StrAlloc(100);StrPCopy(AppTitle, Application.Title);Result := True;Mutex := Windows.CreateMutex(nil, False, AppTitle);if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then beginResult := False;SetWindowText(Application.Handle, '');PrevInstHandle := FindWindow(nil, AppTitle);if PrevInstHandle <> 0 then beginif IsIconic(PrevInstHandle) thenShowWindow(PrevInstHandle, SW_RESTORE)elseBringWindowToTop(PrevInstHandle);SetForegroundWindow(PrevInstHandle);end;if Mutex <> 0 thenMutex := 0;end;StrDispose(AppTitle);end;procedure DestroyMutex;beginif Mutex <> 0 thenCloseHandle(Mutex);end;
      

  5.   

    如何防止一个程序执行两次这是一个比较简单的防止程序执行两次的方法
    implementation 
    var hnd: THandle;initialization
        hnd := CreateMutex(nil, True, 'irgendwaseinmaliges');
        if GetLastError = ERROR_ALREADY_EXISTS then Halt;finalization
        if hnd <> 0 then CloseHandle(hnd);
    end.
      

  6.   

    以上程序放在FORM的ON CREATE事件中。