Board index » cppbuilder » About the mutex...

About the mutex...


2008-01-07 09:18:52 AM
cppbuilder37
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...

Hi Robert
Robert Huang says:
Quote
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...

Thank you for your suggestion:)
It work!!
 

{smallsort}

Re:About the mutex...

"Robert Huang" < XXXX@XXXXX.COM >wrote in message
Quote
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.
Quote
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