Solved

Mutex - Singleton Help

Posted on 2004-10-14
13
3,018 Views
Last Modified: 2008-02-07
am using the following piece of code to ensure that my application only runs once, however I have a few questions about it.

      static Mutex m_Mutex; << in c# I assume that when the methods are static, so are the private members.

      public static void Run(Form mainForm)
      {
         if(IsFirstInstance())
         {
            Application.ApplicationExit += new EventHandler(OnExit);
            Application.Run(mainForm);
         }
      }
     
The following is causing me some confusion, when the application starts it creates a newly named Mutex. It then calls WaitOne and determines whether it receives any messages, if it does it returns true otherwise false. My questions are this, when the next application comes to create a new mutex with the same name, what exactly happens? Does it deny it therefore this is why the WaitOne does not receive any messages, also the timespan is set to zero, how can the mutex determine if any messages have been recieved in zero time?

      static bool IsFirstInstance()
      {
         m_Mutex = new Mutex(false,"SingletonApp Mutext");
         bool owned = false;
         owned = m_Mutex.WaitOne(TimeSpan.Zero,false);
         return owned ;
      }
0
Comment
Question by:rocky050371
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 12305979
I have given a solution to a similar problem here:
http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20633466.html#8628426

unfortunately i cant test your code as i dont have VS.Net right now, you can find TAD's comment very useful for using the singleton way to avoid the multiple instanced of the same application and yes private members can also be static

0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12306218
You need to use this Mutex constructor:

bool createdNew;
m_Mutex = new Mutex(true,"SingletonApp Mutext", out createdNew);

now, if createdNew is true, you created the mutex and own it; if it is false, the mutex already existed. No need to do a WaitOne() for this situation.
0
 

Author Comment

by:rocky050371
ID: 12306300
Thanks, but I was actually after an explanation of exactly what happens, then I may possibly choose an alternate root
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 14

Expert Comment

by:AvonWyss
ID: 12306309
Well, what happens is that the second app will not be able to get the mutex ownership. And that's what this flag which is returned does indicate.
0
 

Author Comment

by:rocky050371
ID: 12306373
Yes, but it is the WaitOne with a Zero Time Period which is slightly confusing.
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 250 total points
ID: 12306439
Well, it returns the state of the mutex, that is, if you own it or not. Is has nothing to do with messages. If you specify a timeout, it will wait up to the time specified to gain the mutex ownership. If this fails, if will return false; if you have the ownership, it will return true.
0
 

Author Comment

by:rocky050371
ID: 12306509
Thank you
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12306527
Rocky, what would you have expected to give an A grade? What were my answers missing?
0
 

Author Comment

by:rocky050371
ID: 12306596
Perhaps a more detailed description.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12306600
Ok, thank you for your feedback.
0
 

Expert Comment

by:pointeman
ID: 22761952
Q. Would m_Mutex.Release need to be implemented as well?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 22762101
I don't understand your question. The mutex has to remain allocated for the duration of the application, otherwise a second instance would not see the mutex as being in use. When the process is closed, the mutex will be released automatically. Therefore, while it may be good practice to release the mutex upon application exit, you must no do this earlier or the second instance detection will fail.
0
 

Expert Comment

by:pointeman
ID: 22763654
Automatically, I get it, thanks
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question