Click here to Skip to main content
12,883,390 members (39,141 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


17 bookmarked
Posted 19 Mar 2011

Run Only One Copy Of Application

, 6 Nov 2011 CPOL
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 | MCTS | CP MVP | Crazy Learner

An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies. He holds Master in Computer Application and Bachelor degree of CS in Information Technology.

He always excited and keen Interested in learning and sharing knowledge. He loves to learn new things, listen music, taking arts and Playing Games..

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Tejas_Vaishnav27-Jun-12 3:36
memberTejas_Vaishnav27-Jun-12 3:36 
GeneralRe: My vote of 5 Pin
RaviRanjankr27-Jun-12 4:34
mvpRaviRanjankr27-Jun-12 4:34 
GeneralRe: No the boolean can be either user or the person using code s... Pin
charles henington25-Apr-11 11:24
membercharles henington25-Apr-11 11:24 
GeneralRe: I'm confused. First you tell me both implementations are the... Pin
AspDotNetDev22-Apr-11 15:27
protectorAspDotNetDev22-Apr-11 15:27 
I'm confused. First you tell me both implementations are the same, now you tell me they are different. Only, you tell me they are different in a different way than what I was talking about. Namely, your implementation accepts a boolean from the user and that boolean can be used to override the single-instance functionality (i.e., it can be used to allow multiple instances at the discretion of the user of your class). While that is potentially useful, it has nothing to do with why I prefer RaviRanjankr's approach.
GeneralRe: A new instance of form will not run if value is set to true ... Pin
charles henington22-Apr-11 6:09
membercharles henington22-Apr-11 6:09 
GeneralRe: Notice the return statement that occurs before the Form1 con... Pin
AspDotNetDev19-Apr-11 12:03
protectorAspDotNetDev19-Apr-11 12:03 
GeneralReason for my vote of 5 Nice and simple. I like it. Pin
Collin Jasnoch7-Nov-11 11:02
memberCollin Jasnoch7-Nov-11 11:02 
GeneralRe: Thanks Collin :) Pin
RaviRanjankr7-Nov-11 19:16
mvpRaviRanjankr7-Nov-11 19:16 
GeneralReason for my vote of 3 prefer non null return values Pin
chrisKennedy19-May-11 13:58
memberchrisKennedy19-May-11 13:58 
GeneralReason for my vote of 5 its easy to use Pin
RK_DBA14-Apr-11 17:42
memberRK_DBA14-Apr-11 17:42 
GeneralReason for my vote of 5 good-good, very good. Pin
ashishkumar00823-Mar-11 19:51
memberashishkumar00823-Mar-11 19:51 
GeneralHmm... just a quick thought: regarding Windows 7 (and perhap... Pin
Nenad L.22-Mar-11 3:08
memberNenad L.22-Mar-11 3:08 
GeneralI think I prefer this technique over the original because th... Pin
AspDotNetDev19-Mar-11 18:15
protectorAspDotNetDev19-Mar-11 18:15 
GeneralRe: it's the exact same code just reworded To include as class f... Pin
charles henington19-Apr-11 11:36
membercharles henington19-Apr-11 11:36 
GeneralTrue that is another way to do the same thing. The reason th... Pin
charles henington19-Mar-11 6:37
membercharles henington19-Mar-11 6: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by RaviRanjanKr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid