Wednesday, December 27, 2017

MUTEX in FPC

http://forum.lazarus.freepascal.org/index.php?topic=36280.0

  1. PROGRAM UNIQUEWAVE;
  2.  {$MODE OBJFPC}{$H+}
  3.  USES
  4.   Interfaces,
  5.   SysUtils,
  6.   Forms,
  7.   Windows,
  8.   uUNIQUEWAVE;
  9.   {$R *.RES}
  10.  CONST
  11.   MutexName = 'UNIQUEWAVE MuTeX';
  12.  VAR
  13.   hMutex: THandle;
  14. BEGIN
  15.  Try
  16.   hMutex:= CreateMutex(Nil, True, MutexName);
  17.    If GetLastError = ERROR_ALREADY_EXISTS
  18.    Then
  19.     Begin
  20.      CloseHandle(hMutex);
  21.      Exit;
  22.     End;
  23.   Try
  24.    RequireDerivedFormResource:= True;
  25.     Application.Title:= 'UNIQUEWAVE';
  26.     Application.Initialize;
  27.      wndGUI:= TwndGUI.Create(Application); //no need for a TaskbarButton
  28.      wndGUI.Show;
  29.     Application.Run;
  30.   Finally
  31.    If hMutex <> 0
  32.    Then CloseHandle(hMutex);
  33.   End;
  34.  Except
  35.   On E: Exception
  36.   Do wndGUI.Log('MAIN PRG'+sLineBreak+E.ClassName+sLineBreak+E.Message, True);
  37.  End;
  38. END.



program MutexExample;

uses
  Vcl.Forms, Vcl.Dialogs,
  System.SyncObjs, // defines TMutex
  Winapi.Windows,  // defines ERROR_SUCCESS
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

var
  AppMutex: TMutex = nil;
  MutexErr: Integer = 0;

procedure CheckSecondInstance;
const
  // any unique name will work--I chose to create a GUID
  UNIQUE_MUTEX_NAME = '{AD12094E-7308-4067-8CAC-565A1FF3ADDC}';
begin
  // try to create a mutex; don't need any security rights
  AppMutex := TMutex.Create(nil, True, UNIQUE_MUTEX_NAME);

  // check to see if it was successful
  MutexErr := GetLastError;

  // if not, previous instance of application already created the mutex
  if MutexErr <> ERROR_SUCCESS then
    ShowMessage(Application.Title + ' is already running.');
end;

begin
  // initialize app like normal, but don't start it yet
  Application.Initialize;
  Application.Title := 'Simple Mutex Example';

  // run our check here
  CheckSecondInstance;
  try
    // if the mutex was successfully created, this is the first instance of this app
    if MutexErr = ERROR_SUCCESS then begin
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end;
  finally
    // if a mutex was successfully created (and our app ran),
    // be sure to free it to release the mutex
    if Assigned(AppMutex) then
      AppMutex.Free;
  end;
end.

Šta reći, a ne zaplakati od sreće. 
Mutex, pa još u Lazarusu, pa radi lol.
PROGRAM UNIQUEWAVE;
 {$MODE OBJFPC}{$H+}
 
 USES
  Interfaces,
  SysUtils,
  Forms,
  Windows,
  uUNIQUEWAVE;
 
  {$R *.RES}
 
 CONST
  MutexName = 'UNIQUEWAVE MuTeX';
 
 VAR
  hMutex: THandle;
 
BEGIN
 Try
  hMutex:= CreateMutex(Nil, True, MutexName);
 
   If GetLastError = ERROR_ALREADY_EXISTS
   Then
    Begin
     CloseHandle(hMutex);
     Exit;
    End;
 
  Try
   RequireDerivedFormResource:= True;
    Application.Title:= 'UNIQUEWAVE';
    Application.Initialize;
     wndGUI:= TwndGUI.Create(Application); //no need for a TaskbarButton
     wndGUI.Show;
    Application.Run;
  Finally
   If hMutex <> 0
   Then CloseHandle(hMutex);
  End;
 Except
  On E: Exception
  Do wndGUI.Log('MAIN PRG'+sLineBreak+E.ClassName+sLineBreak+E.Message, True);
 End;
END.
 

MOŽE BITI SAMO JEDAN !
STARTUJTE SAMO JEDNU INSTANCU SVOJE APLIKACIJE !
KORISTITE MUTEX TEHNOLOGIJU !
Start only one instance of your application !
Use Mutex in Lazarus !

The Highlander: There can be only one



No comments:

Post a Comment

Коментар: