Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

Run Only One Copy Of Application

, 6 Nov 2011
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

Software Developer
India India
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
He keep himself on the desk of his imagination, hanging around with some inceptions.
Follow on   Twitter   Google+

Comments and Discussions

GeneralMy vote of 5 PinmemberTejas_Vaishnav27-Jun-12 3:36 
GeneralRe: My vote of 5 PinmvpRaviRanjankr27-Jun-12 4:34 
GeneralRe: No the boolean can be either user or the person using code s... Pinmembercharles henington25-Apr-11 11:24 
No the boolean can be either user or the person using code snippet, however the person using the snippet decides. you can hard code the boolean value to always return true or always false, return true if certain conditions are met such as the commandline arguments meet a certain criteria, or if say settings value or xml value are set to either true or false then it will allow or not allow second instance based on the supplied criteria. they are different in the sense that it will allow the value to be different based on the boolean value but they are the same as in the sense that they will both prevent a new instance from starting making it a single instance the boolean must be set to true.
GeneralRe: I'm confused. First you tell me both implementations are the... PinprotectorAspDotNetDev22-Apr-11 15:27 
GeneralRe: A new instance of form will not run if value is set to true ... Pinmembercharles henington22-Apr-11 6:09 
GeneralRe: Notice the return statement that occurs before the Form1 con... PinprotectorAspDotNetDev19-Apr-11 12:03 
GeneralReason for my vote of 5 Nice and simple. I like it. PinmemberCollin Jasnoch7-Nov-11 11:02 
GeneralRe: Thanks Collin :) PinmvpRaviRanjankr7-Nov-11 19:16 
GeneralReason for my vote of 3 prefer non null return values PinmemberchrisKennedy19-May-11 13:58 
GeneralReason for my vote of 5 its easy to use PinmemberRK_DBA14-Apr-11 17:42 
GeneralReason for my vote of 5 good-good, very good. Pinmemberashishkumar00823-Mar-11 19:51 
GeneralHmm... just a quick thought: regarding Windows 7 (and perhap... PinmemberNenad L.22-Mar-11 3:08 
GeneralI think I prefer this technique over the original because th... PinprotectorAspDotNetDev19-Mar-11 18:15 
GeneralRe: it's the exact same code just reworded To include as class f... Pinmembercharles henington19-Apr-11 11:36 
GeneralTrue that is another way to do the same thing. The reason th... Pinmembercharles henington19-Mar-11 6:37 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid