自己做了个小程序,但发现project.exe居然能同时运行多个.且多次同时运行的时候会发生严重错误.
想在程序中进行控制,如果project.exe已经运行的话,再运行project.exe应提示:project.exe已经运行,不能再运行了.
修改工程文件如下:
program Project1;
uses
Forms,
Windows,
Unit1 in Unit1.pas {Form1};
{$R *.res}
var
hMutex:hWnd;
begin
Application.Initialize;
Application.Title:=test;
hMutex:=CreateMutex(nil,false,test);
if GetLastError<>Error_Already_Exists then
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
Application.MessageBox(本程序只允许同时运行一个,Error);
ReleaseMutex(hMutex);
end;
end.