Click here to Skip to main content
12,449,440 members (60,750 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.6K views
2 bookmarked
Posted

Run Only One Copy Of Application

, 12 Apr 2011 CPOL
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

You may also be interested in...

Pro
Pro

Comments and Discussions

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 13 Apr 2011
Article Copyright 2011 by KevinPorter
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid