Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a WPF application is already running? [duplicate]

Possible Duplicate:
What is the correct way to create a single instance application?

How can I check if my application is already open? If my application is already running, I want to show it instead of opening a new instance.

like image 383
Irakli Lekishvili Avatar asked Aug 24 '11 22:08

Irakli Lekishvili


2 Answers

Here is one line code which will do this for you...

 if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
// Show your error message
}
like image 146
Bathineni Avatar answered Sep 24 '22 09:09

Bathineni


public partial class App
    {
        private const string Guid = "250C5597-BA73-40DF-B2CF-DD644F044834";
        static readonly Mutex Mutex = new Mutex(true, "{" + Guid + "}");

        public App()
        {

            if (!Mutex.WaitOne(TimeSpan.Zero, true))
            {
                //already an instance running
                Application.Current.Shutdown();
            }
            else
            {
                //no instance running
            }
        }
    }
like image 45
HotN Avatar answered Sep 24 '22 09:09

HotN