c# Single instance of an application on one machine

Some times you want one running instance for your application then you can deny another running instance with this code.

using System;
using System.Threading;
using System.Windows.Forms;
namespace MyApp
    static class Program
        static Mutex _Mutex = null;
        static void Main()
            bool myAppFirstInstance;
            _Mutex = new Mutex(true, "Global\\MyApp ", out myAppFirstInstance);
            if (myAppFirstInstance)
                Application.Run(new MainForm());
                MessageBox.Show("There is already exists a running instance of this application",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

About bpostaci
Escalation Engineer in Microsoft.

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 )

Connecting to %s

%d bloggers like this: