http://forum.lazarus.freepascal.org/index.php?topic=36280.0
- 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.
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
Komentar=