How To: Check if application is already running#

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

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;
      }
   }
}

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

Sunday, April 02, 2006 8:53:40 AM (GMT Daylight Time, UTC+01:00) #    Comments [1]  | 

 

All content © 2010, Keith Rull
On this page
This site
Calendar
<April 2006>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
Archives
Sitemap
Blogroll OPML
Disclaimer

Powered by: newtelligence dasBlog 2.3.9074.18820

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts


Pick a theme: