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

4

11

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.

Topper answered 24/8, 2011 at 22:12 Comment(1)
Is it one instance per computer, per user or per desktop?Rachmaninoff
U
21
[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(...);
    }
}
Undulation answered 24/8, 2011 at 22:21 Comment(3)
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 applicationTopper
This code should go to the main method. Look at here for more information about main method. joyfulwpf.blogspot.com/2009/05/…Undulation
use System.Windows.Application.Current.Shutdown(); instead of retrunSalicylate
B
4

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

 if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
// Show your error message
}
Bumgarner answered 25/8, 2011 at 9:0 Comment(1)
This is the actual answer. Just put this in Application_Startup and you are good to go.Wappes
F
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
            }
        }
    }
Fenugreek answered 24/8, 2011 at 22:25 Comment(1)
does not work for my .NET 4.0 WPF app?Unrounded
P
-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);
    }
Polyhistor answered 24/8, 2011 at 22:25 Comment(4)
This won't show the existing instance's windowRegrate
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.Regrate
@Chris- Thank you for your attention I didn't read question carefully.Polyhistor
Does not work for my .NET 4.0 WPF appUnrounded

© 2022 - 2024 — McMap. All rights reserved.