Click here to Skip to main content
15,892,809 members
Articles / Programming Languages / C#
Article

Preventing Multiple Application Instances When Using Application.Restart

Rate me:
Please Sign up or sign in to vote.
1.63/5 (18 votes)
27 Jun 2008CPOL 34.4K   14   9
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

C#
// 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)


Written By
Software Developer Zonal Retail Data Systems
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
martinfwf16-Oct-12 23:53
martinfwf16-Oct-12 23:53 
GeneralIt is working fine. Pin
seenit9-Aug-11 17:22
seenit9-Aug-11 17:22 
GeneralWhere Application.restart will come Pin
Satyen0517-Mar-11 3:35
Satyen0517-Mar-11 3:35 
GeneralRe: Where Application.restart will come Pin
therutman9-Aug-11 18:51
therutman9-Aug-11 18:51 
GeneralNice Pin
johannesnestler30-Apr-08 4:27
johannesnestler30-Apr-08 4:27 
GeneralAnother Way Pin
kjsteuer29-Apr-08 3:39
kjsteuer29-Apr-08 3:39 
GeneralRe: Another Way Pin
therutman29-Apr-08 22:35
therutman29-Apr-08 22:35 
GeneralRe: Another Way Pin
kjsteuer30-Apr-08 3:49
kjsteuer30-Apr-08 3:49 
GeneralRe: Another Way Pin
amiel_h11-Aug-09 6:51
amiel_h11-Aug-09 6:51 

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

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