2013年12月29日 星期日

delphi mutex


//single mode
program
 Project1;

uses
  Windows,
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

const
  MutexName = 'onlyone_MUTEX';
  ClassName = 'TForm1';
  WindowName = 'Form1';
var
  Mutex: THandle;
  Handle: THandle;
begin
  Mutex := OpenMutex(MUTEX_ALL_ACCESS, False, MutexName);
  if Mutex <> 0 then
  begin
    Handle := FindWindow(ClassName, WindowName);
    SetForegroundWindow(Handle);
    Exit;
  end;
  CreateMutex(nil, False, MutexName);
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

沒有留言:

張貼留言