Prevent multiple instances of wpf application

How to build a single instance application in WPF.
In a wpf application, How could I prevent opening multiple windows ?

Very Easy way, go to App.xaml.cs and coding like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  private void Application_Startup(object sender, StartupEventArgs e)
        {
            Process thisProc = Process.GetCurrentProcess();
            if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
            {
                MessageBox.Show("Application running");
                Application.Current.Shutdown();
                return;
            }
 
            var wLogin = new LoginWindow();
 
            if (wLogin.ShowDialog() == true)
            {
                var wMain = new Main();
                wMain.WindowState = WindowState.Maximized;
                wMain.Show();
            }
            else
            {
                Application.Current.Shutdown();
            }      
        }


Views (1946)

Tags: , ,

One Comment to "Prevent multiple instances of wpf application"

  1. SANTHOSH KUMAR says:

    Hi,

    The solution you suggested is working for me. Thank you for this.

    Cheers,
    Santhosh

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

SoftCodeArticle was formed to help developers solve common software issues.