Solved

Mutex - Singleton Help

Posted on 2004-10-14
13
3,019 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 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