var hMutex: Hwnd;
Ret: integer;
begin
Application.Initialize;
Application.Title:='Program Run Once Test':
hMutex:=CreateMutex(nil,false,'Program Run Once Test');
Ret:=GetLastError;
if Ret<>Error_Already_Exists then
begin
Application.Create(TForm1,Form1);
Application.Run;
end
else
Application.MessageBox('This Program is running','Warning',MB_OK);
ReleaseMutex(hMutex);
end;
Ret: integer;
begin
Application.Initialize;
Application.Title:='Program Run Once Test':
hMutex:=CreateMutex(nil,false,'Program Run Once Test');
Ret:=GetLastError;
if Ret<>Error_Already_Exists then
begin
Application.Create(TForm1,Form1);
Application.Run;
end
else
Application.MessageBox('This Program is running','Warning',MB_OK);
ReleaseMutex(hMutex);
end;
本文介绍了一种使用互斥量确保程序仅运行一个实例的方法。通过创建一个名为 'Program Run Once Test' 的互斥量,并检查 GetLastError 函数返回值来判断程序是否已经运行。如果程序未运行,则创建并显示主窗体;若已运行,则弹出警告消息。

1万+

被折叠的 条评论
为什么被折叠?



