DCSIMG
C# Close previous application instance - Asaf Shelly

C# Close previous application instance

Published Tuesday, March 02, 2010 5:44 PM

Continuing this post:

http://kseesharp.blogspot.com/2008/11/c-check-if-application-is-already.html

It is possible to close the currently running instance of the application:

 

      static bool CloseApplicationAlreadyRunning()
      {
         string proc = Process.GetCurrentProcess().ProcessName;
         Process[] processes = Process.GetProcessesByName(proc);
         if (processes.Length < 2) return (false);
         foreach (Process process in processes)
         {
            if (process.Id != Process.GetCurrentProcess().Id)
            {
               process.CloseMainWindow();
               if (process.WaitForExit(6000)) return (false);
               else return (true);
            }
         }
         return false;
      }

 

Modify the timeout appropriately.

 

Asaf

http://asyncop.com/

תגים:

Comments

# Jasper said on Thursday, March 04, 2010 10:11 AM

Can you show an example when new process does not close the old one but bring it to front ?

# Asaf Shelly said on Tuesday, March 09, 2010 5:56 PM

Continuing the following post and as an answer to Jasper: blogs.microsoft.co.il/.../asafshelly

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: