Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mutex - Singleton Help

Posted on 2004-10-14
13
Medium Priority
?
3,024 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 19

Expert Comment

by:Fahad Mukhtar
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 750 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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