只能打开一个程序 用互斥CreateMutex来运行唯一实例

//用互斥CreateMutex来运行唯一实例
{$R *.res}
var
  mutex:HWND;
  exitHWND:HWND;
begin

  if(ParamCount<5) then
  begin
    ShowMessage('参数不正确');
    Exit;
  end;
  RootGroupID:=StrToIntDef(ParamStr(1),-1);
  if(RootGroupID<0) then
  begin
    ShowMessage('输入的分组ID错误');
     Exit;
  end;
  RootGroupName:=ParamStr(2);
  HttpToken:=ParamStr(3);
  GQNumber:=ParamStr(4);
  ApiServer:=ParamStr(5);
  
  mutex:= CreateMutex(nil,False,PWideChar('GQCacheDemo'+inttostr(RootGroupID)));//用互斥来运行唯一实例
 if(GetLastError=ERROR_ALREADY_EXISTS) then
 begin
    exitHWND:=FindWindow(nil,PWideChar('GQ监控中心提醒--当前分组:'+RootGroupName));
    ShowWindow(exitHWND,SW_RESTORE);
    Exit;
 end;

    Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
例子1:用互斥CreateMutex来运行唯一实例
 
例子2:View Code  

 

//============网友的一个 方法 复杂: 失控的心跳  815936792 

type
  TMyMutex=class(TObject)
  private
    MutHandle: THandle;
    MessageId: Cardinal;
    SaveOnMessage: TMessageEvent;
  protected
    procedure BroadCastFocusMessage;
    procedure OnAppMessage(var Msg: TMsg; var Handled: Boolean);
  public
    PrevMutexExists: Boolean;
    constructor Create(const UniqueAppStr: string);
    destructor Destory;
  end;

{$R *.RES}

const
  MI_QUERYWINDOWHANDLE=1;
  MI_RESPONDWINDOWHANDLE=2;
  MI_ERROR_NONE=0;
  MI_ERROR_FAILSUBCLASS=1;
  MI_ERROR_CREATINGMUTEX=2;

constructor TMyMutex.Create(const UniqueAppStr: string);
begin
  SaveOnMessage:=Application.OnMessage;
  Application.OnMessage:=OnAppMessage;
  MessageId := RegisterWindowMessage(PChar(UniqueAppStr));
  MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, PChar(UniqueAppStr));
  if MutHandle=0 then
  begin
    PrevMutexExists:=False;
    MutHandle := CreateMutex(nil, False, PChar(UniqueAppStr));
  end
  else
  begin
    PrevMutexExists:=True;
    BroadCastFocusMessage;
  end;
end;

destructor TMyMutex.Destory;
begin
  if MutHandle<>0 then CloseHandle(MutHandle);
  Application.OnMessage:=SaveOnMessage;
end;

procedure TMyMutex.OnAppMessage(var Msg: TMsg;var Handled: Boolean);
begin
  if Msg.message=MessageId then
  begin
    case Msg.wParam of
      MI_QUERYWINDOWHANDLE:
        begin
          if IsIconic(Application.Handle) or
             (Assigned(Application.MainForm) and not IsWindowVisible(Application.MainForm.Handle)) then
          begin
            ShowWindow(Application.Handle, SW_MINIMIZE);
            ShowWindow(Application.Handle, SW_RESTORE);
          end;
          SetActiveWindow(Application.Handle);
          SetForegroundWindow(Application.Handle);
          if Msg.lParam<>0 then
            PostMessage(HWND(Msg.lParam), MessageId, MI_RESPONDWINDOWHANDLE, Application.Handle);
        end;
      MI_RESPONDWINDOWHANDLE:
        begin
          SetForegroundWindow(HWND(Msg.lParam));
          if Assigned(Application) then
            Application.Terminate;
        end;
    end;
  end
  else
    if Assigned(SaveOnMessage) then SaveOnMessage(Msg, Handled);
end;

procedure TMyMutex.BroadCastFocusMessage;
var
  BSMRecipients: DWORD;
begin
  BSMRecipients := BSM_APPLICATIONS;
  BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients, MessageId, MI_QUERYWINDOWHANDLE, Application.Handle);
end;

var
  MyMutex: TMyMutex;
begin
  Application.Initialize;
  MyMutex:=TMyMutex.Create(UniqueAppStr);
  if not MyMutex.PrevMutexExists then
  begin
    Application.CreateForm(TfrmMain, frmMain);
    DateSeparator :='-';
  ShortDateFormat :='yyyy-mm-dd';
  LongDateFormat :='yyyy-mm-dd';       //FormatSettings.
 // Application.CreateForm(TfrmHolidayDlg1, frmHolidayDlg1);
 // Application.CreateForm(TfrmAddWeekOthDlg, frmAddWeekOthDlg);
 // Application.CreateForm(TfrmAddHolidayDlg, frmAddHolidayDlg);
 // Application.CreateForm(TfrmAddHolidayDlg, frmAddHolidayDlg);
  Application.Run;
  end;
  MyMutex.Free;
end.

  

posted @ 2015-04-16 15:34  海蓝7  阅读(577)  评论(0)    收藏  举报