Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Run Only One Copy Of Application

, 12 Apr 2011
Rate this:
Please Sign up or sign in to vote.
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
 
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)

Share

About the Author

KevinPorter
Software Developer (Junior) Employment Law Advisory Services Ltd
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 13 Apr 2011
Article Copyright 2011 by KevinPorter
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid