K
kjzzn
Unregistered / Unconfirmed
GUEST, unregistred user!
在看一些防止程序第二次运行的代码时,发现会有稍微的不同。
例一:
var
hAppMutex: THandle;
begin
Application.Initialize;
hAppMutex := CreateMutex(nil, false, PChar('asdfghjkl'));
if (hAppMutex = 0) then
exit;
if (hAppMutex <> 0) and (GetLastError() = ERROR_ALREADY_EXISTS) then
begin
releasemutex(hAppMutex);
Exit;
end;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm:=False;
Application.Run;
CloseHandle(hAppMutex);
例二:
var
hAppMutex: THandle;
begin
Application.Initialize;
hAppMutex := CreateMutex(nil, false, PChar('asdfghjkl'));
if (hAppMutex = 0) or (GetLastError() = ERROR_ALREADY_EXISTS) then
Exit;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm:=False;
Application.Run;
CloseHandle(hAppMutex);
比较发现差一个 releasemutex(hAppMutex);
测试时发现没有什么不同,这里releasemutex是必须的么?请高手指点
例一:
var
hAppMutex: THandle;
begin
Application.Initialize;
hAppMutex := CreateMutex(nil, false, PChar('asdfghjkl'));
if (hAppMutex = 0) then
exit;
if (hAppMutex <> 0) and (GetLastError() = ERROR_ALREADY_EXISTS) then
begin
releasemutex(hAppMutex);
Exit;
end;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm:=False;
Application.Run;
CloseHandle(hAppMutex);
例二:
var
hAppMutex: THandle;
begin
Application.Initialize;
hAppMutex := CreateMutex(nil, false, PChar('asdfghjkl'));
if (hAppMutex = 0) or (GetLastError() = ERROR_ALREADY_EXISTS) then
Exit;
Application.CreateForm(TForm1, Form1);
Application.ShowMainForm:=False;
Application.Run;
CloseHandle(hAppMutex);
比较发现差一个 releasemutex(hAppMutex);
测试时发现没有什么不同,这里releasemutex是必须的么?请高手指点