Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Only One Copy Of Application

0.00/5 (No votes)
13 Apr 2011 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here