DevPinoy.org
A Filipino Developers Community
   
How To: Check if application is already running

Here's a short code that checks to see if the current application is already running.

[code language="C#"]

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace KeithRull.CS.Windows.CheckIfAppIsAlreadyRunning
{
   class Program
   {
      static void Main(string[] args)
      {
         if (IsAppAlreadyRunning())
         {
            Console.WriteLine("This application is already running... Press any key to exit.");
         }
         else
         {
            Console.WriteLine("App now running!");
            // ... do whatever you want to do here.
         }
         Console.ReadLine();
      }

      public static bool IsAppAlreadyRunning()
      {
         bool isAlreadyRunning = false;
         Process currentProcess = Process.GetCurrentProcess();
         Process[] processes = Process.GetProcesses();
         foreach (Process process in processes)
         {
            if (currentProcess.Id != process.Id)
            {
               if (currentProcess.ProcessName == process.ProcessName)
               {
                  isAlreadyRunning = true;
               }
            }
         }
         return isAlreadyRunning;
      }
   }
}

[/code]

Nothing really special... just my thoughts for today.


Posted 04-01-2006 11:47 PM by keithrull
Filed under:

Comments

cvega wrote re: How To: Check if application is already running
on 04-02-2006 7:13 PM
Just a quick note: When you are running a loop to search for single item (i.e., like in your loop seeking specific process), you can break the loop after you'd found your match, this is to ensure only one from the collection is matched, and breaking means saving extra nanoseconds (depending on how much work is in the loop).

foreach (Process process in processes)
{
 if (currentProcess.Id != process.Id)
 {
   if (currentProcess.ProcessName ==
       process.ProcessName)
   {
       isAlreadyRunning = true;
       break;

       // Or better, return immidiately:
       // return true;
   }
 }
}

Cheers,

-chris
keithrull wrote re: How To: Check if application is already running
on 04-02-2006 7:55 PM
ahhhh! i didn't see that! :P
cvega wrote re: How To: Check if application is already running
on 04-02-2006 8:29 PM
By the way, on Windows XP SP1/SP2 and Windows 2003, there is an additional function in kernel32.dll called GetProcessId, this is to make things easier when finding running process:

[DllImport("kernel32.dll")]
static extern int GetProcessID(IntPtr handle);

public bool IsProcessRunning(Process process)
{
return GetProcessID(process.Handle)!=0;
}

No Find/FindNext, no callback enumeration, no looping, no mutexes, no singleton, no undocumented api (NtQueryInformationProcess), and it's faster since it accesses the internal kernel process table --> But will work on Windows XP SP1/SP2 or Windows 2003 operating systems only.

Cheers,

-chris
jokiz wrote re: How To: Check if application is already running
on 04-03-2006 1:51 AM
not to argue with the win32 master but i'd rather not use interop for this simple scenario...
CryptoKnight wrote re: How To: Check if application is already running
on 04-12-2006 12:41 AM
You can also do this...

public static bool IsAppAlreadyRunning()
     {
        Process currentProcess = Process.GetCurrentProcess();
        return Process.GetProcessesByName(currentProcess.Name).Length > 0;
     }

;-)
Sjef wrote re: How To: Check if application is already running
on 05-09-2008 4:55 AM

It should be (at least for .Net 1.1) :

public static bool IsAppAlreadyRunning()

{

Process currentProcess = Process.GetCurrentProcess();

return Process.GetProcessesByName(currentProcess.ProcessName).Length > 1;

}

Copyright DevPinoy 2005-2008