Click here to Skip to main content
15,885,939 members
Articles / Desktop Programming / Windows Forms
Alternative
Tip/Trick

Run Only One Copy Of Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
12 Apr 2011CPOL 6.7K   2   2
How about using a Mutex? This should also work for Terminal Services sessions. Use in Program.csstatic Mutex mut;try{ bool isOwned = false; mut = new Mutex(true, Application.ProductName + " MUTEX: {53A4988C-F91F-4054-9076-220AC5EC03F3}", out isOwned); if (!isOwned)...
How about using a Mutex? This should also work for Terminal Services sessions. Use in Program.cs

C#
static Mutex mut;

try
{
    bool isOwned = false;
    mut = new Mutex(true, Application.ProductName + " MUTEX: {53A4988C-F91F-4054-9076-220AC5EC03F3}", out isOwned);

    if (!isOwned)
    {
        MessageBox.Show(Application.ProductName + " is already running on this machine.",
                         Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }

    Application.Run(new Form1());
}
finally
{
    try
    {
        mut.ReleaseMutex();
    }
    catch (ApplicationException)
    {
    }
}

License

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


Written By
Software Developer (Junior) Employment Law Advisory Services Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy Post the original tip/trick does use Mutex and where does... Pin
charles henington12-Apr-11 6:34
charles henington12-Apr-11 6:34 
GeneralRe: Updated the example now. Also didn't noticed your example us... Pin
KevinPorter12-Apr-11 23:27
KevinPorter12-Apr-11 23:27 

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.