Sunday, December 2, 2012

How to: Check if another instance of the application is already running

how it is possible to check whether another instance of the program is running and if so stop the application before loading if there is an existing instance of it.

Solution:


write at your Main method the following code to quit the method which will kill the currently loading process instantly.

if (System.Diagnostics.Process.GetProcessesByName(
 System.IO.Path.GetFileNameWithoutExtension(
  System.Reflection.Assembly.GetEntryAssembly().Location)).Length > 1)
 {
  MessageBox.Show("Application is already running");
  System.Diagnostics.Process.GetCurrentProcess().Kill();
 
            

No comments:

Post a Comment