比如有程序main.exe,如果限制只能运行一个实例,就是不允许打开几个同时运行的那种!

解决方案 »

  1.   

     CreateMutex(nil, false, 'Client');
      if GetLastError = ERROR_ALREADY_EXISTS  then
      begin
        MessageBox(Application.Handle,'已经在运行!','提示信息',MB_OK+MB_ICONINFORMATION);
    end加到工程文件里
      

  2.   

     var   
          Mutex:   THandle;   
      begin   
          Mutex   :=   CreateMutex(nil,   true,   'Test');   
          if   GetLastError   <>   ERROR_ALREADY_EXISTS   then   
          begin   
              Application.Initialize;   
              Application.CreateForm(TfmMain,   fmMain);   
              Application.Run;   
          end   
          else   
              MessageBox(0,   '系统已经正在运行!',   '请注意......',   48);   
          ReleaseMutex(Mutex);   
        
      end.
      

  3.   

    也可以用全局原子做
    project Test;uses
      Windows,
      Forms,
      mainUnit in 'mainUnit.pas' {FormMain};{$R *.res}var
      appAtom : THandle;
    begin  Application.Initialize;  if 0 = GlobalFindAtom('SOME-UNIQUE-TEXT-RELATED-TO-THIS-APPLICATION') then
      begin
        appAtom := GlobalAddAtom('SOME-UNIQUE-TEXT-RELATED-TO-THIS-APPLICATION') ;    try
          Application.CreateForm(TFormMain, FormMain) ;
          Application.Run;
        finally
          GlobalDeleteAtom(appAtom);
        end
      end
      else
      begin
        Application.MessageBox('程序已运行!') ;
      end;