Re: Detecting a running application - FAQ Change?
From: Bruce Roberts (ber_at_bounceitattcanada.xnet)
Date: 08/30/04
- Next message: Rob Kennedy: "Re: Detecting a running application"
- Previous message: Bruce Roberts: "Re: TStringgrid - autocalculate"
- In reply to: David Reeve: "Re: Detecting a running application"
- Next in thread: L D Blake: "Re: Detecting a running application - FAQ Change?"
- Reply:(deleted message) L D Blake: "Re: Detecting a running application - FAQ Change?"
- Reply: Maarten Wiltink: "Re: Detecting a running application - FAQ Change?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 30 Aug 2004 12:20:28 -0400
"David Reeve" <dree4456@big-pond.net.au> wrote in message
news:bURXc.10409$D7.10274@news-server.bigpond.net.au...
> Now this bit raises some queries....
>
> finally
> ReleaseMutex (hMyMutex);
> end;
>
> Alerted to it by MemProof, I checked the return of this function. It
fails
> because windows reckons the thread doesn't own it, and thus can't release
> it. Why? On stepping the value of hMyMutex is still as allocated by
create.
> The create function was called with bInitialOwner param set True.
>
> But, taking a slightly wider view, why release ownership, when what you
want
> to do is close the handle?
> And further to this, why close the handle? Help is pretty clear that the
> handle is closed when the app terminates.
The ReleaseMutext call should be changed to CloseHandle. From a purely
astetic view and because it promotes good programming habits it only makes
sense to explicitly release any resources that have been explicitly aquired.
More subtly, there may be a fair bit of program teardown that follows and
ISTM that the system shouldn't be blocked from starting a new instance
during that process. If it should, then I'd suggest that the mutex has been
created at the wrong point and doesn't preceed the code its trying to
protect.
The suggestion has also been made that the CreateMutex bInitialOwner
parameter be changed. But no one has proposed any actual code. So I will
// uses Windows, SysUtils
var hMyMutex : tHandle;
begin
hMyMutex := CreateMutex (nil, False,
pChar (Uppercase (ExtractFileName
(Application.ExeName))
if (hMyMutex = 0) or (GetLastError = error_Already_Exists) then
Application.Terminate
try
// the code maintained by Delphi
Application.Initialize;
. . .
finally
CloseHanlde (hMyMutex);
end;
end;
// Original from current FAQ
// uses Windows, SysUtils
var hMyMutex : tHandle;
begin
hMyMutex := CreateMutex (nil, True,
pChar (Uppercase (ExtractFileName
(Application.ExeName))
if (hMyMutex = 0) or (GetLastError = error_Already_Exists) then
Application.Terminate
try
// the code maintained by Delphi
Application.Initialize;
. . .
finally
ReleaseMutex (hMyMutex);
end;
end;
- Next message: Rob Kennedy: "Re: Detecting a running application"
- Previous message: Bruce Roberts: "Re: TStringgrid - autocalculate"
- In reply to: David Reeve: "Re: Detecting a running application"
- Next in thread: L D Blake: "Re: Detecting a running application - FAQ Change?"
- Reply:(deleted message) L D Blake: "Re: Detecting a running application - FAQ Change?"
- Reply: Maarten Wiltink: "Re: Detecting a running application - FAQ Change?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]