Click here to Skip to main content
15,562,816 members
Articles / Desktop Programming / Windows Forms
Posted 19 Mar 2011

Tagged as


16 bookmarked

Run Only One Copy Of Application

Rate me:
Please Sign up or sign in to vote.
4.92/5 (22 votes)
6 Nov 2011CPOL
Yet another way to this is as follows: using System;using System.Collections.Generic;using System.Windows.Forms;using System.Threading;namespace OnlyOneInstance{ static class Program { [STAThread] static void Main() { bool...
Yet another way to this is as follows:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace OnlyOneInstance
    static class Program
        static void Main()
            bool instantiated;
          /* If instantiated is true, this is the first instance of the application; else, another instance is running. */
            Mutex mutex = new Mutex(true, "UniqueID", out instantiated);
            if (!instantiated)
                MessageBox.Show("Already Running");
            Application.Run(new Form1());

Take a look there[^] to read the complete article.

To download the sample code, you can go here[^]
After downloading, go to bin directory and then debug OnlyOneInstance.exe file twice, you will see a message box which states Program Already Running.


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

Written By
Software Developer
India India
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
Tejas Vaishnav27-Jun-12 4:36
professionalTejas Vaishnav27-Jun-12 4:36 
My 5 , Nice so simply explain.
GeneralRe: My vote of 5 Pin
RaviRanjanKr27-Jun-12 5:34
professionalRaviRanjanKr27-Jun-12 5:34 
GeneralRe: No the boolean can be either user or the person using code s... Pin
charles henington25-Apr-11 12:24
charles henington25-Apr-11 12:24 
GeneralRe: I'm confused. First you tell me both implementations are the... Pin
AspDotNetDev22-Apr-11 16:27
protectorAspDotNetDev22-Apr-11 16:27 
GeneralRe: A new instance of form will not run if value is set to true ... Pin
charles henington22-Apr-11 7:09
charles henington22-Apr-11 7:09 
GeneralRe: Notice the return statement that occurs before the Form1 con... Pin
AspDotNetDev19-Apr-11 13:03
protectorAspDotNetDev19-Apr-11 13:03 
GeneralMessage Removed Pin
7-Nov-11 12:02
professionalN_tro_P7-Nov-11 12:02 
GeneralRe: Thanks Collin :) Pin
RaviRanjanKr7-Nov-11 20:16
professionalRaviRanjanKr7-Nov-11 20:16 
GeneralReason for my vote of 3 prefer non null return values Pin
chrisKennedy19-May-11 14:58
chrisKennedy19-May-11 14:58 
GeneralReason for my vote of 5 its easy to use Pin
RK_DBA14-Apr-11 18:42
RK_DBA14-Apr-11 18:42 
GeneralReason for my vote of 5 good-good, very good. Pin
ashishkumar00823-Mar-11 20:51
ashishkumar00823-Mar-11 20:51 
GeneralHmm... just a quick thought: regarding Windows 7 (and perhap... Pin
Nenad L.22-Mar-11 4:08
Nenad L.22-Mar-11 4:08 
GeneralI think I prefer this technique over the original because th... Pin
AspDotNetDev19-Mar-11 19:15
protectorAspDotNetDev19-Mar-11 19:15 
GeneralRe: it's the exact same code just reworded To include as class f... Pin
charles henington19-Apr-11 12:36
charles henington19-Apr-11 12:36 
GeneralTrue that is another way to do the same thing. The reason th... Pin
charles henington19-Mar-11 7:37
charles henington19-Mar-11 7:37 

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.