Click here to Skip to main content
Licence CPOL
First Posted 28 Apr 2008
Views 16,492
Bookmarked 14 times

Preventing Multiple Application Instances When Using Application.Restart

By | 27 Jun 2008 | Article
Use the Mutex class with a timeout to prevent more than one instance of an application running in a situation where Application.Restart is being called.

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



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIt is working fine. Pinmemberseenit17:22 9 Aug '11  
GeneralWhere Application.restart will come PinmemberSatyen053:35 17 Mar '11  
GeneralRe: Where Application.restart will come Pinmembertherutman18:51 9 Aug '11  
GeneralNice Pinmemberjohannesnestler4:27 30 Apr '08  
GeneralAnother Way Pinmemberkjsteuer3:39 29 Apr '08  
GeneralRe: Another Way Pinmembertherutman22:35 29 Apr '08  
GeneralRe: Another Way Pinmemberkjsteuer3:49 30 Apr '08  
GeneralRe: Another Way Pinmemberamiel_h6:51 11 Aug '09  

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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