unit ufrm_Mutex;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfrmMutex =
class(TForm)
btnThreadLoop: TButton;
btnSynCheck: TButton;
Memo1: TMemo;
procedure btnThreadLoopClick(Sender: TObject);
procedure btnSynCheckClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private-Deklarationen }
public
Mutex : THandle;
end;
var
frmMutex: TfrmMutex;
implementation
{$R *.dfm}
//==============================================================================
procedure TfrmMutex.btnSynCheckClick(Sender: TObject);
begin
Memo1.Lines.add('
Before Enter');
if WaitForSingleObject(
Mutex, INFINITE) <> WAIT_OBJECT_0
then
RaiseLastOSError;
try
finally
ReleaseMutex(
Mutex)
end;
Memo1.Lines.add('
After Enter');
end;
//==============================================================================
procedure TfrmMutex.btnThreadLoopClick(Sender: TObject);
var
tout : Cardinal;
begin
if WaitForSingleObject(
Mutex, INFINITE) <> WAIT_OBJECT_0
then
RaiseLastOSError;
try
Memo1.Lines.add('
Loop Begin');
tout := GetTickCount + 15000;
while GetTickCount < tOut
do begin
sleep(1);
end;
// while
finally
ReleaseMutex(
Mutex)
end;
Memo1.Lines.add('
Loop End');
end;
//==============================================================================
procedure TfrmMutex.FormCreate(Sender: TObject);
var
FSA: SECURITY_ATTRIBUTES;
FSD: SECURITY_DESCRIPTOR;
begin
InitializeSecurityDescriptor(@FSD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@FSD, true,
nil, false);
FSA.lpSecurityDescriptor := @FSD;
FSA.nLength := sizeof(SECURITY_ATTRIBUTES);
FSA.bInheritHandle := false;
Mutex := CreateMutex(@FSA, true, PAnsiChar('
Mutex1') );
Memo1.Lines.add('
MutexHandle: ' + inttostr(
Mutex) );
end;
//==============================================================================
procedure TfrmMutex.FormDestroy(Sender: TObject);
begin
CloseHandle(
Mutex);
end;
end.