您的位置:首页 > 其它

判断程序是否已经运行,然后kill旧的或者新的.

2007-02-02 10:36 253 查看
1.利用 CreateMutex

CreateMutex(nil, false, 'project');
result := true;
if GetLastError = ERROR_ALREADY_EXISTS then
begin
// SendMessage(MSG_CLOSE_WIN, RegisterWindowMessage('project'), 0, 0); // HWND_BROADCAST
close;
ShowMessage('You have the program running allready!!');
Halt(0);
result := false; // true false
end;

2.利用SendMessage和FindWindow(FindWindow(类名,caption),)

SendMessage(FindWindow('TfrmSimulator',nil), WM_CLOSE, 0 ,0); // Simulator

3.GlobalFindAtom (用此函数一定得销毁)

{搜寻表看是否程序已运行}
if GlobalFindAtom('PROGRAM_RUNNING') = 0 then
{ 如果没运行,则在表中增加信息 }
atom := GlobalAddAtom('PROGRAM_RUNNING')
else begin
{ 如果程序已运行,则显示信息然后退出 }
MessageDlg('You have the program running
all ready!!', mtWarning,[mbOK], 0);
Halt;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{从表中移走信息以便程序能再运行}
GlobalDeleteAtom(atom);
end;

如果是想关闭以前的然后再打开新的建议用2.建议3最好不要用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐