Solved

Mutex - Singleton Help

Posted on 2004-10-14
13
3,013 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks, but I was actually after an explanation of exactly what happens, then I may possibly choose an alternate root
0
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:rocky050371
Comment Utility
Thank you
0
 
LVL 14

Expert Comment

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

Author Comment

by:rocky050371
Comment Utility
Perhaps a more detailed description.
0
 
LVL 14

Expert Comment

by:AvonWyss
Comment Utility
Ok, thank you for your feedback.
0
 

Expert Comment

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

Expert Comment

by:AvonWyss
Comment Utility
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
Comment Utility
Automatically, I get it, thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now