🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #904

Re: About the mutex...

#904
From: Asger Joergensen
Date: Mon, 07 Jan 2008 01:59
51 lines
2402 bytes
Hi Robert
 
 Robert Huang says:
 
> When A is running, the message "Already in running!!" will appear.
> But when A is terminated, the message will still appear.
> How do I fix the problem?

The corrections in the code below is just an idea, 
and I have not tested it.

Kind regards
Asger

//---------------------------------------------------------------------------
void __fastcall TBForm::ButtonExeClick(TObject *Sender)
{
    HANDLE TargetMutex = CreateMutex(NULL, false, "Program-A");
    if(GetLastError() == ERROR_ALREADY_EXISTS)
    {
       ShowMessage("Already in running!!");
    }else 
    if(TargetMutex){
       ReleaseMutex(TargetMutex);
       CloseHandle(TargetMutex);
	 WinExec(ExeATitle.c_str(), SW_SHOW);
    }     
}
//---------------------------------------------------------------------------


And in program-A will be like this:


HANDLE Mutex;
//---------------------------------------------------------------------------
__fastcall TAForm::TAForm(TComponent* Owner)
        : TForm(Owner)
{
    Mutex = CreateMutex(NULL, false, "Program-A");
   // shouldn't You be doing some checking here ??
}
//---------------------------------------------------------------------------
void __fastcall TAForm::FormClose(TObject *Sender, TCloseAction &Action)
{
    ReleaseMutex(Mutex);
    CloseHandle(Mutex);
    Action = caFree;
}
//---------------------------------------------------------------------------
Technical Details
Message-ID: <MPG.21eba5f09151a10a9899cd@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!eweka.nl!lightspeed.eweka.nl!194.134.4.77.MISMATCH!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <47817e00@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,34037fef0fe917a3
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Asger Joergensen <junk@Asger-P.dk>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: About the mutex...
Date: Mon, 7 Jan 2008 02:59:29 +0100
Message-ID: <MPG.21eba5f09151a10a9899cd@newsgroups.borland.com>
References: <47817e00@newsgroups.borland.com>
Organization: Asger-P
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
User-Agent: MicroPlanet-Gravity/2.70.2067
NNTP-Posting-Host: 83.73.201.25
X-Trace: newsgroups.borland.com 1199671287 83.73.201.25 (6 Jan 2008 18:01:27 -0700)
Lines: 52
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!eweka.nl!lightspeed.eweka.nl!194.134.4.77.MISMATCH!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail