Click here to Skip to main content
Click here to Skip to main content

Preventing Multiple Application Instances When Using Application.Restart

By , 27 Jun 2008
 

Introduction

This article outlines a way to prevent multiple instances of an application running on a machine, using the Mutex class, in situations where Application.Restart gets called.

Background

The current application I am working on requires that only one instance be running on any one machine at any time, a reasonably common requirement. It seems the two main ways to accomplish this involve iterating through the running processes or creating a Mutex. However, the application I work with restarts itself in several situations (using Application.Restart). In the majority of cases, Application.Restart will start a new instance of the application before the old AppDomain has finished closing. Without allowing a timeout on the lock of the Mutex, the new instance will not start, and the old instance will finish closing, leaving you with nothing running.

Code

// Declare the Mutex. If it exists we get a reference to the existing one
// if not, the OS will create a new one. 
// false indicates we do not want initial ownership of the mutex
// mutexName is the unique name of the mutex
static Mutex _mutex = new Mutex(false, "mutexName");

[STAThread]
static void Main()
{
   // WaitOne will return true if it is able to lock the Mutex and false if the mutex
   // has already been locked by another instance of the application.
   // 1000 is the timeout in milliseconds - this is what gives the old instance of 
   // the application time to shut down and is arbitrary.
   // the boolean specifies whether to exit the synchronization domain before the wait
   if (!_mutex.WaitOne(1000, false))
      return;


   // Application code in here


   // This is required so that other instances can now lock the Mutex.
   _mutex.ReleaseMutex();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

therutman
Software Developer Zonal Retail Data Systems
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermartinfwf16 Oct '12 - 23:53 
GeneralIt is working fine.memberseenit9 Aug '11 - 17:22 
GeneralWhere Application.restart will comememberSatyen0517 Mar '11 - 3:35 
GeneralRe: Where Application.restart will comemembertherutman9 Aug '11 - 18:51 
GeneralNicememberjohannesnestler30 Apr '08 - 4:27 
GeneralAnother Waymemberkjsteuer29 Apr '08 - 3:39 
GeneralRe: Another Waymembertherutman29 Apr '08 - 22:35 
GeneralRe: Another Waymemberkjsteuer30 Apr '08 - 3:49 
GeneralRe: Another Waymemberamiel_h11 Aug '09 - 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 27 Jun 2008
Article Copyright 2008 by therutman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid