Run Only One Copy Of Application

This Article allow you to run only one Instance of application at a time.
Many time we have requirement that we need to run only one instance of application at a time. If you have same requirement then in that case this article might be useful for you.
Here you will learn how to handle only one instance of application by using Mutex

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);
        }
    }
}

Reference Link:- ClickHere to Reference Link
Click here to Download SampleCode.
After Downloading SampleCode, go to the bin directory and then click on OnlyOneInstance.Exe file twice.
you will see a message box is prompted with a message box that Stated Program is “Already Running”

Enjoy Coding 🙂

Advertisements

About Ravi Ranjan Kumar

An Indian who Living, Loving & Learning Technology with different tastes and willing to share knowledge and thoughts.
This entry was posted in .Net, C#, Tips&Tricks and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s