🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by Robert Huang Mon, 07 Jan 2008 01:18
About the mutex...
#903
Author: Robert Huang
Date: Mon, 07 Jan 2008 01:18
42 lines
2101 bytes
Hi, all.
I have a question about the mutex.
I want to execute program-A by B, so I place the code in program-B like 
this:


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


And in program-A will be like this:


HANDLE Mutex;
//---------------------------------------------------------------------------
__fastcall TAForm::TAForm(TComponent* Owner)
        : TForm(Owner)
{
    Mutex = CreateMutex(NULL, false, "Program-A");
}
//---------------------------------------------------------------------------
void __fastcall TAForm::FormClose(TObject *Sender, TCloseAction &Action)
{
    ReleaseMutex(Mutex);
    Action = caFree;
}
//---------------------------------------------------------------------------


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?

Thx:) 
Re: About the mutex...
#904
Author: 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;
}
//---------------------------------------------------------------------------
Re: About the mutex...
#905
Author: Robert Huang
Date: Mon, 07 Jan 2008 11:31
2 lines
1035 bytes
Thank you for your suggestion:)
It work!! 
Re: About the mutex...
#772
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Jan 2008 17:54
43 lines
2304 bytes
"Robert Huang" <remmurds.bbs@ptt.cc> wrote in message 
news:47817e00@newsgroups.borland.com...

>    ReleaseMutex(Mutex);

That will not free the mutex.  It will only release the lock that program A 
may have on it.  You need to use CloseHandle() to actually free the mutex.

> When A is running, the message "Already in running!!" will
> appear. But when A is terminated, the message will still appear.

Neither program A or B are closing their respective handles to the mutex. 
All calls to Create/OpenMutex() must be balanced with equal calls to 
CloseHandle().  Since program B is not closing its handle to the mutex, the 
mutex remains alive after program A has terminated.

    void __fastcall TBForm::ButtonExeClick(TObject *Sender)
    {
        HANDLE TargetMutex = OpenMutex(SYNCHRONIZE, FALSE, "Program-A");
        if( !TargetMutex )
            WinExec(ExeATitle.c_str(), SW_SHOW);
        else
        {
            CloseHandle(TargetMutex); // <-- add this!
            ShowMessage("Already in running!!");
        }
    }


    __fastcall TAForm::TAForm(TComponent* Owner)
        : TForm(Owner)
    {
        Mutex = CreateMutex(NULL, FALSE, "Program-A");
    }

    __fastcall TAForm::~TAForm()
    {
        CloseHandle(Mutex);
    }


Gambit 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads