怎样在Console Application 实现程序只能运行一个.例如:我双击一个叫notepad.exe已经运行了,我再双击notepad.exe在任务管理器里面
也只有一个notepad.exe-------------------
我在Application里面就可以,
但是在Console Application 就不行了
也只有一个notepad.exe-------------------
我在Application里面就可以,
但是在Console Application 就不行了
但是在Console Application 就不行了
-----------------------------------请把“在Application里面就可以”的代码贴出来看一下。对症下药嘛
SysUtils, windows;
//~~~~~~begin
CreateMutex(nil, False, PChar('你的互斥标识字符串'));
if GetLastError = ERROR_ALREADY_EXISTS then Exit;
//加上面两句就行了
readln;
end.
失败就说明已经有instance运行了
begin
Result := OpenMutex(MUTEX_ALL_ACCESS, false, PChar(MutexName)) = 0;
end;
// 检查互斥,保证仅有一个实例存在。
if CheckMutex(RES_MutexName) then
begin
// 建立互斥标识。
hMutex := CreateMutex(nil, true, PChar(RES_MutexName));
Application.Initialize;
Application.CreateForm(TfMain, fMain);
Application.Run;
// 销毁互斥标识
ReleaseMutex(hMutex);
end;