Click here to Skip to main content
13,344,765 members (55,956 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.1K views
17 bookmarked
Posted 19 Mar 2011

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
    {
       
        [STAThread]
        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");
               return;
            }
            Application.Run(new Form1());
            GC.KeepAlive(mutex);
        }
    }
}


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.

License

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

Share

About the Author

RaviRanjanKr
Software Developer
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: A new instance of form will not run if value is set to true ... Pin
charles henington22-Apr-11 7:09
membercharles henington22-Apr-11 7:09 

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
Web03 | 2.8.180111.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by RaviRanjanKr
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid