Anda di halaman 1dari 7

Untuk sinkronisasi semacam itu ada banyak cara di Windows.

bisa pake
Critical Section, bisa juga pake Mutex.
Selain itu Delphi menyediakan suat class yang cool, namanya:
TMultiReadExclusiveWriteSynchronizer. Dengan class ini memungkinkan beberapa
thread membaca (read only) ke suatu object dapat melakukannya bersamaan
tanpa menunggu, tetapi jika ada satu thread yang ingin menulis (write only
atau read/write) object tersebut dia harus menunggu semua thread yang
membacanya selesai. Saat sebuah thread mendapat hak untuk menulis, semua
thread yang lain -- baik untuk membaca maupun menulis -- harus menunggu.
Berbeda dengan Critical Section ataupun Mutex yang baik untuk menulis maupun
untuk membaca sebuah thread mendapat hak eksklusif, tidak efisien jika
banyak thread yang hanya perlu membaca ke object tersebut karena jika hanya
dibaca sebuah object menjadi tidak kritis.
Untuk menggunakan Critical Section:
-deklarasikan sebuah variable global -- terhadap fungsi/method yang
memanggilnya -- bertipe TRTLCriticalSection
-inisialisasikan variable tsb dengan: InitializeCriticalSection(variablenya)
-setiap sebelum mengakses array tsb, panggil:
EnterCriticalSection(variablenya)
-setiap setelah mengakses array tsb, panggil:
LeaveCriticalSection(variablenya)
-gunakan try-finally mechanism untuk mencegak locked condition jika terjadi
exception pada saat mengakses array tsb.
-destroy variable TRTLCriticalSection sebelum keluar dari program
Untuk menggunakan Mutex
-deklarasikan sebuah variable global -- terhadap fungsi/method yang
memanggilnya -- bertipe THandle, mis: var mutex:THandle;
-inisialisasikan variable tsb dengan: mutex := CreateMutex(nil,false,nil);
-setiap sebelum mengakses array tsb, panggil: if
WaitForSingleObject(mutex,INFINITE)=WAIT_OBJECT_0 then ...
-setiap setelah mengakses array tsb, panggil: ReleaseMutex(mutex);
-gunakan try-finally mechanism untuk mencegak locked condition jika terjadi
exception pada saat mengakses array tsb.
-destroy variable mutex sebelum keluar dari program
Untuk menggunakan TMultiReadExclusiveWriteSynchronizer
-deklarasikan sebuah variable global -- terhadap fungsi/method yang
memanggilnya -- bertipe TMultiReadExclusiveWriteSynchronizer, mis: var
sync:TMultiReadExclusiveWriteSynchronizer;
-inisialisasikan variable tsb dengan: sync :=
TMultiReadExclusiveWriteSynchronizer.Create;
-setiap sebelum mengakses array tsb, panggil: sync.BeginWrite -- untuk
write-access atau sync.BeginRead -- untuk read-access
-setiap setelah mengakses array tsb, panggil: sync.EndWrite atau
sync.EndRead (sesuai dengan Begin...-nya)
-gunakan try-finally mechanism untuk mencegak locked condition jika terjadi
exception pada saat mengakses array tsb.
-destroy variable sync sebelum keluar dari program
Selamat mencoba
program Project1;

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

{$R *.res}

begin
CreateMutex(nil , true , 'Object Mutex');
if GetLastError = ERROR_ALREADY_EXIST then
begin
ShowMessage('Aplikasi sudah di buka');
end;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
procedure TForm1.FormShow(Sender : TObject);
var
atom : integer;
CRLF : string;
begin
if GlobalFindAtom('THIS_IS_SOME_OBSCUREE_TEXT') = 0 then
atom := GlobalAddAtom('THIS_IS_SOME_OBSCUREE_TEXT')
else
begin
CRLF := #10 + #13;
ShowMessage(Program ini hanya bisa dijalankan satu kali' + CRLF +
Reboot windows untuk menjalankannya kembali , atau...' + CRLF +
Meregister Program !!');
Close;
end;
end;
Uses Windows

procedure TForm1.FormCreate(Sender: TObject);
var appID:THandle;
begin
appID:=CreateMutexA(nil, false, PChar('my_app_ID'));
if (appID<>0) and (GetLastError = ERROR_ALREADY_EXISTS ) then begin
ShowMessage('Tidak Dapat Menjalankan Aplikasi Ini 2x,
Silahkan Tutup Aplikasi Yang Sudah Berjalan Terlebih Dahulu.');
Halt;
end;
end;
CreateMutex(nil, True, 'test');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageDlg('Program anda sudah running', mtInformation, [mbOK], 0);
halt;
end;
program Project1;
uses
Forms, Windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
var
PreviousHandle : THandle;
begin
PreviousHandle := FindWindow('TForm1','Form1');
if PreviousHandle = 0 then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
SetForegroundWindow(PreviousHandle);
end.
program Project1;
uses
Forms, Windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
var
Mutex : THandle;
begin
Mutex := CreateMutex(nil, True, 'My_Unique_Application_Mutex_Name');
if (Mutex = 0) OR (GetLastError = ERROR_ALREADY_EXISTS) then
begin
// code to searh for, and activate
// the previous (first) instance
end
else
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
if Mutex <> 0 then
CloseHandle(Mutex);
end;
end;
end.
function LookForPreviousInstance
(Handle : THandle; Param : Cardinal): boolean; stdcall;
var
buf : array[0..255] of char;
WindowModuleName : string;
WindowClassName : string;
wih : THandle; // window instance handle
begin
Result := True;

GetClassName(Handle, buf, SizeOf(buf));
WindowClassName := buf;

//form class name match!
if WindowClassName = TForm1.ClassName then
begin
wih := GetWindowLong(Handle, GWL_HINSTANCE),

GetModuleFileName(wih, buf, SizeOf(buf));
WindowModuleName := buf;

//from the "same" application?
if WindowModuleName = ThisModuleName then
begin
FoundIt := True;
FoundHandle := Handle;
//stop the enumeration
Result := False;
end;
end;
end;
program Project1;
uses
Forms, Windows, Messages,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
var
Mutex : THandle;
begin
MyMsg := RegisterWindowMessage('My_Unique_App_Message_Name');
Mutex := CreateMutex(nil, True, 'My_Unique_Application_Mutex_Name');
if (Mutex = 0) OR (GetLastError = ERROR_ALREADY_EXISTS) then
begin
SendMessage(HWND_BROADCAST, MyMsg, 0, 0);
end
else
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
if Mutex <> 0 then
CloseHandle(Mutex);
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
MyMsg: Cardinal;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMessage := AppMessage;
end;
procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
if Msg.Message = MyMsg then
begin
Application.Restore;
SetForeGroundWindow(Application.MainForm.Handle);
Handled := true;
end;
end;
end.
Uses Windows
02
03 procedure TForm1.FormCreate(Sender: TObject);
04 var appID:THandle;
05 begin
06 appID:=CreateMutexA(nil, false, PChar('my_app_ID'));
07 if (appID<>0) and (GetLastError = ERROR_ALREADY_EXISTS ) then begin
08 ShowMessage('Tidak Dapat Menjalankan Aplikasi Ini 2x,
09 Silahkan Tutup Aplikasi Yang Sudah Berjalan Terlebih Dahulu.');
10 Halt;
11 end;
12 end;
var
mHandle: THandle; // Mutexhandle
initialization
mHandle := CreateMutex(nil, True, 'XYZ');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
ShowMessage('Program is already running!');
halt;
end;
finalization
if mHandle <> 0 then CloseHandle(mHandle)
end.
procedure TForm1.FormCreate(Sender: TObject);
var
atom: Integer;
CRLF: string;
begin
if GlobalFindAtom('A Text used to be stored in memory') = 0 then
atom := GlobalAddAtom('A Text used to be stored in memory')
else
begin
CRLF := #10 #13;
ShowMessage('This version may only be run once for every Windows Session.'
CRLF
'To run this program again, you need to restart Windows, or better yet:'
CRLF
'REGISTER !!');
Close;
end;
end;
begin
if MutexIsExists(OurMutexID) then
Terminate
else begin
CreateMutex(OurMutexID);
Goto MainProgram;
DestroyMutex;
end;
end;
program SingleInstance;
uses
Windows,
Forms,
Dialogs,
Main_SingleInstance in 'Main_SingleInstance.pas' {Form1};
{$R *.res}
const
MutexName = 'CodeCall.Net Mutex';
resourcestring
DUPLICATE_MSG = 'Another instance of this program is already running. This
one will shutdown';
var
vMutex: THandle;
begin
// try to create our mutex
vMutex := CreateMutex(nil, True, MutexName);
// check if the previous line caused error and the error is ERROR_ALREADY_EXIS
TS
if GetLastError = ERROR_ALREADY_EXISTS then
begin
// show the duplication message
MessageDlg(DUPLICATE_MSG, mtError, [mbOk], 0);
Exit; // terminates the program
end;
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
// release the mutex
CloseHandle(vMutex);
end;
end.

Anda mungkin juga menyukai