Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Goal:
Open many programs which include same functions.
I implement the function from first program, and the other programs also want to implement the function.
They can use the function orderly till the function of first program have done.

Tool:
I found the "Mutex" can fulfil the purpose.

Meathod:
I programmed an easy program to test the function about the Mutex.
Opened two same programs(if they are A and B).
When I pressed the Button1 of A, it will count 5 seconds and show on screen.
If I pressed the Button1 of B inside the 5 seconds, it will starts to count till the A has done.
The result conformed with my anticipation!

BUT!!!
Sometimes, the B program crashed and showed "No response" on the windows.
I can use the B till the counter of A has done.
I can't find the question...

The following code is main code which I coded by MFC.
I hope someone can give me some suggestions.
Thanks a lot!!

C++
CMutex mutex(false,_T("CodeProject"));  //declare a named mutex

void CMutextestDlg::OnBnClickedButton1()
{
     mutex.Lock(); //Lock the thread
     
     //Count for 5 seconds.
     CString str;
     for(int i=0;i<=5;i++)
     {	
          str.Format(_T("%d"),i);
	  SetDlgItemText(IDC_STATIC,str);
	  Sleep(1000);
     }
     SetDlgItemText(IDC_STATIC,_T("End")); //Counter completed

     mutex.Unlock(); //Unlock the thread
}
Posted
Comments
Maciej Los 16-Mar-15 7:08am    
Why to use mutex? I'd suggest to check for already opened instance of program first...
Timberbird 16-Mar-15 7:23am    
I suspect that could be because you block the thread and thus prevent your application from handling Windows messages completely. To check if that's the reason try to increase interval to eg 15 seconds and see if you get "No response" message more often :).

If that's the case, you obviously would need to lock for any finite amount of time (say 1 second), then check result and either proceed with your cycle (if lock obtained successfully) or let your application process events (when lock couldn't be obtained - for example, check this question).

Also, if you're planning to run many instances of your application, consider adding Global namespace prefix to your mutex name - to make sure it'll be visible from all user sessions

1 solution

Mutex lock calls are blocking calls. The execution of the entire program is blocked there until it can achieve a lock. For a dialog based application, locking on an event handler is a big no-no. You can still do something similar, you just can't do it on the same thread that handles the GUI updates and other windows messages.
 
Share this answer
 
Comments
Member 10307999 17-Mar-15 23:12pm    
Thanks Holguin for your reply!
But I can't understand absolutely...
Would you please revise my code or show another sample example to me?
So appreciated!
Albert Holguin 18-Mar-15 10:07am    
I can't revise your code because the whole idea there is bad. Probably the best way for you to test this out is to use a non-gui application. That would prevent your blocking call from being a problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900