Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Apr : Mutex Issue
| Subject: | Mutex Issue |
| Posted by: | "Jesse" (nospamplease@msdlg.com) |
| Date: | Mon, 16 Apr 2007 10:03:38 |
I'm trying to change my application so that it will allow it to be run only
once on the same PC. When it runs the second time, I want it to detect any
other running instance, if it's minimized, restore it, then close down
(properly) this instance. I've started using the Mutex function to do
this, however, I have two problems with it that I can't seem to resolve. 1)
It detects the other instance, but does not restore it. 2) The new instance
seems to go away, because I don't see it on the task bar, however, if I got
to the Task Manager/Processes, I can see the 2nd instance running there, so
it's not shutting down properly. Here is the code the I'm using:
First, in my .dpr file, I have the following code:
...
var
Mutex : THandle;
begin
MyMsg := RegisterWindowMessage('CIMS');
Mutex := CreateMutex(nil, false, 'CIMS');
if (Mutex = 0) OR (GetLastError = ERROR_ALREADY_EXISTS) then
Begin
MessageBox(GetActiveWindow,pChar('C.I.M.S. is already
running'),'',MB_OK + MB_ICONINFORMATION);
SendMessage(HWND_BROADCAST, MyMsg, 0, 0);
end
else
Begin
// code to initialize & run the pap.
end;
end;
Now, in my main form, I have the following:
...
type
...
private
{ Private declarations }
procedure NewWindowProc(var Msg: TMsg; var Handled: Boolean);
public
{ Public declarations }
end;
var
MainForm: TMainForm;
MyMsg: Cardinal;
...
procedure TMainForm.NewWindowProc(var Msg: TMsg; var Handled: Boolean);
Begin
if Msg.Message = MyMsg then
begin
Application.Restore;
SetForeGroundWindow(Application.MainForm.Handle);
Handled := true;
end;
end;
Am I missing something here?
Thanks,
Jesse