?
Solved

Mutex - Singleton Help

Posted on 2004-10-14
13
Medium Priority
?
3,023 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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