-
Notifications
You must be signed in to change notification settings - Fork 14
/
Core.Startup.pas
55 lines (43 loc) · 1.07 KB
/
Core.Startup.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
unit Core.Startup;
interface
uses
Winapi.Windows;
type
TMutexLocker = class
private
class var FName: string;
class var FMutex: THandle;
class var FIsLocked: Boolean;
class var FIsExist: Boolean;
public
class procedure Init(Name: string; Locked: Boolean = False);
class function Lock: Boolean;
class procedure Unlock;
class property IsLocked: Boolean read FIsLocked;
class property IsExist: Boolean read FIsExist;
end;
implementation
{ TMutexLocker }
class procedure TMutexLocker.Init(Name: string; Locked: Boolean);
begin
FName := Name;
FMutex := 0;
FIsLocked := False;
FIsExist := False;
if Locked then Lock;
end;
class function TMutexLocker.Lock: Boolean;
begin
FMutex := CreateMutex(nil, True, LPCTSTR(FName));
FIsLocked := FMutex <> 0;
FIsExist := GetLastError() = ERROR_ALREADY_EXISTS;
Result := FIsLocked;
end;
class procedure TMutexLocker.Unlock;
begin
if FMutex <> 0 then
CloseHandle(FMutex);
FIsLocked := False;
FIsExist := False;
end;
end.