program Project1;uses Forms,windows, //use windows Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin CreateMutex(nil, True, 'MyAppClass'); if GetLastError = ERROR_ALREADY_EXISTS then begin MessageBox(0, '程序已经运行', '88!' ,MB_ICONERROR); Halt; end; Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.
var wnd:hwnd; begin wnd:=findwindow(nil,'ZW123'); IF wnd <>0 THEN begin showmessage('程序已运行!,只能运行一次!'); case isiconic(wnd) of true:showwindow(wnd,sw_restore); false:begin showwindow(wnd,SW_showNORMAL);setforegroundwindow(wnd);end; end; application.Terminate; end; FORM1.Caption:='ZW123'; end;
如何防止一个程序执行两次这是一个比较简单的防止程序执行两次的方法 implementation var hnd: THandle;initialization hnd := CreateMutex(nil, True, 'irgendwaseinmaliges'); if GetLastError = ERROR_ALREADY_EXISTS then Halt;finalization if hnd <> 0 then CloseHandle(hnd); end.
或
CreateMapFile()
Forms,windows, //use windows
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin CreateMutex(nil, True, 'MyAppClass');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '程序已经运行', '88!' ,MB_ICONERROR);
Halt;
end; Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
wnd:hwnd;
begin
wnd:=findwindow(nil,'ZW123');
IF wnd <>0 THEN
begin
showmessage('程序已运行!,只能运行一次!');
case isiconic(wnd) of
true:showwindow(wnd,sw_restore);
false:begin showwindow(wnd,SW_showNORMAL);setforegroundwindow(wnd);end;
end;
application.Terminate;
end;
FORM1.Caption:='ZW123';
end;
implementation
var hnd: THandle;initialization
hnd := CreateMutex(nil, True, 'irgendwaseinmaliges');
if GetLastError = ERROR_ALREADY_EXISTS then Halt;finalization
if hnd <> 0 then CloseHandle(hnd);
end.