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最好不要用