none
How to close(if instance of a windows form application is already running), or restart windows form application RRS feed

  • Question

  • Hello guys.

    My name is Gyan Parkash.

    i made a windows application(C# in visual Studio 2010). the application is successfully runs when i double click on the file type associated with it. The problem is when i double click on the file again ,new instance of the application is launched. I want only one instance of the application should  run at a time.

    the code in main method i used is given below


     static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                if (args.Length > 0)
                {
                     bool instanceCountOne = false;
    
                     using (Mutex mtex = new Mutex(true, "myappname", out instanceCountOne))
                     {
    
                         
                         if (instanceCountOne)
                         {
    			 Form1 f1 = new Form1();
                             Application.Run(f1);
                             mtex.ReleaseMutex();
                         }
                         else
                         {
                             MessageBox.Show("An application instance is already running");
                         }
    
                     }  
                }
    
                else
                {
                    Application.Run(new Form1());
                }
                
            }
        }

    Now this trick only shows that an instance of the application is already running but not how to close the instance already running and start new one.

    can any got any idea ?

    Please help me

    thanks in advance 

    Friday, January 25, 2013 1:48 PM

Answers

  • Hi Gyan,

    You can do this without using Mutex. Mutex servers another purpose. The code bellow in the program.cs file checks whether your application is already running and send a message for it to close.

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            [STAThread]
            static void Main()
            {
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    // replace WindowsFormsApplication1 with your app name!
                    if (clsProcess.ProcessName.Equals("WindowsFormsApplication1"))
                    {
                        clsProcess.CloseMainWindow();
                    }
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    

    Remember: this doesn't force the other instance to quit! It sends a message for the other instance to close. The other instance receives that event (FormClosing) and may choose not to close. The code bellow illustrates this:

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("Should I close?", "Goind down", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == System.Windows.Forms.DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }
    }
    

    Hope this helps.

    Regards,



    Armando Lacerda

    Friday, January 25, 2013 2:38 PM