Sunday, November 7, 2010

WPF - Single Instance Application in WPF

In some scenarios launching multiple copies of the same WPF application is a problem, especially for document-based applications or server applications.

WPF does not provide a native solution for single instance applications.
Let See how to make a Single Instance Application in WPF.

Step 1
Create a WPF Application.

Step 2
Add a following code in app.xaml code behind,it is look like this

 #region Constructor

        public App()
        {
            try
            {
                if (MultipleInstance()) // Check first multiple instance of application
                {
                    MessageBox.Show("More than one instance is running");
                    System.Threading.Thread.Sleep(1000);
                    ProcessKiller(); // Kill the current process
                }
              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);   
            }
        }

        #endregion

        #region Static Methods

        /// <summary>
        /// Check Multiple Instance
        /// </summary>
        /// <returns>Boolean</returns>
        private static Boolean MultipleInstance()
        {
            Boolean Flag = false;
            try
            {
                System.Diagnostics.Process[] ProcessObj = null; ;
                
                // Name of Process module
                String ModualName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName.ToString();

                // Get a process name
                String ProcessName = System.IO.Path.GetFileNameWithoutExtension(ModualName);

                // Get all instances of Current application running on the local computer.

                ProcessObj = System.Diagnostics.Process.GetProcessesByName(ProcessName);

                if (ProcessObj.Length > 1) // if multipal application is running then it is true otherwise it is false
                {
                    Flag = true;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return Flag;
        }

        /// <summary>
        /// kill the current Process
        /// </summary>
        private static void ProcessKiller()
        {
            
            try
            {
                System.Diagnostics.Process ProcessObj = System.Diagnostics.Process.GetCurrentProcess();
                ProcessObj.Kill(); // kill the current process
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
           
        }

        #endregion

Run the project

Download
Download Source Code

2 comments:

  1. Thanks a lot, I adapted your code to my needs,
    copy above InitializeComponent() :

    String ModualName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName.ToString();
    String ProcessName = System.IO.Path.GetFileNameWithoutExtension(ModualName);
    var currProcs = Process.GetProcesses().Where(prc => prc.ProcessName.Contains(ProcessName));
    if (currProcs.Count() > 1)
    {
    MessageBox.Show("Another istance already running!", ProcessName, MessageBoxButton.OK, MessageBoxImage.Information);
    Application.Current.Shutdown(-1);
    return;
    }

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete