10

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.

Community
  • 1
  • 1
Irakli Lekishvili
  • 33,492
  • 33
  • 111
  • 169

4 Answers4

20
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

static void Main() 
{
    Process currentProcess = Process.GetCurrentProcess();
    var runningProcess = (from process in Process.GetProcesses()
                          where
                            process.Id != currentProcess.Id &&
                            process.ProcessName.Equals(
                              currentProcess.ProcessName,
                              StringComparison.Ordinal)
                          select process).FirstOrDefault();
    if (runningProcess != null)
    {
        ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
       return; 
    }
}

Method 2

static void Main()
{
    string procName = Process.GetCurrentProcess().ProcessName;

    // get the list of all processes by the "procName"       
    Process[] processes=Process.GetProcessesByName(procName);

    if (processes.Length > 1)
    {
        MessageBox.Show(procName + " already running");  
        return;
    } 
    else
    {
        // Application.Run(...);
    }
}
Bubbled86
  • 461
  • 2
  • 7
  • 17
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • 1
    thank for replay but it was unclear for me. I dont know where to use this code. in Main window or in app.xaml? main window is opening if log in dialog result = true. I i dont know even how to show, maximize opened application – Irakli Lekishvili Aug 25 '11 at 11:21
  • 1
    This code should go to the main method. Look at here for more information about main method. http://joyfulwpf.blogspot.com/2009/05/where-is-main-method-in-my-wpf.html – CharithJ Aug 25 '11 at 12:08
  • use System.Windows.Application.Current.Shutdown(); instead of retrun – AmirHossein Rezaei May 30 '18 at 08:34
4

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

 if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
// Show your error message
}
Bathineni
  • 3,436
  • 18
  • 25
3
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
            }
        }
    }
HotN
  • 4,216
  • 3
  • 40
  • 51
-1

Do This :

    using System.Threading;
    protected override void OnStartup(StartupEventArgs e)
    {
        bool result;
        Mutex oMutex = new Mutex(true, "Global\\" + "YourAppName",
             out result);
        if (!result)
        {
            MessageBox.Show("Already running.", "Startup Warning");
            Application.Current.Shutdown();
        }
        base.OnStartup(e);
    }
Saber Amani
  • 6,409
  • 12
  • 53
  • 88
  • This won't show the existing instance's window – Chris Shain Aug 24 '11 at 22:27
  • 1
    I just tested it. The first time you run the application, the form shows normally. The second time, you get a dialog saying "Already Running", and when you dismiss that dialog nothing happens. The OP wanted the original instance of the application to be shown in that case. Using your solution, if I minimize the original instance, or put notepad in front of it, it isn't brought to the foreground. – Chris Shain Aug 24 '11 at 22:39
  • 1
    @Chris- Thank you for your attention I didn't read question carefully. – Saber Amani Aug 25 '11 at 07:00
  • Does not work for my .NET 4.0 WPF app –  Feb 15 '13 at 13:38