如何避免一个程序的多次运行?
将该段代码放置在dpr文件中,记住要引用Windows单元。
Application.Title := 'Application Name';
//两处'Application Name'要相一致
hMutex:=CreateMutex(nil,false,'Application Name');
if GetLastError = Error_Already_Exists then
begin
Application.MessageBox('程序已经启动,请检测!','警告',MB_OK);
ReleaseMutex(hMutex);
Application.FreeOnRelease;
end;
Application.Title := 'Application Name';
//两处'Application Name'要相一致
hMutex:=CreateMutex(nil,false,'Application Name');
if GetLastError = Error_Already_Exists then
begin
Application.MessageBox('程序已经启动,请检测!','警告',MB_OK);
ReleaseMutex(hMutex);
Application.FreeOnRelease;
end;
评论
发表评论