4

To prevent user from running multiple instances of my application, I am using this code:-

 Process[] pArry = Process.GetProcesses();
           int nCount = 0;
           foreach (Process p in pArry)
           {
               string ProcessName = p.ProcessName;
               ProcessName = ProcessName.ToLower();
               if (ProcessName.CompareTo("myApp") == 0)
               {
                   nCount++;
               }
           }   
           if (nCount > 1)
           {
               MessageBox.Show(AppAlreadyRunning,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
               Process.GetCurrentProcess().Kill();
           }

But As we know, Process Name changes if change the exe name. So If user changes "myApp.exe" to "UserApp.exe", This patch won't work! Is there any way out?

I am using C# in VS2010. Thanks!

Swanand
  • 4,027
  • 10
  • 41
  • 69

3 Answers3

7

This is very simple process. Calling this function returns a process class with all the information you need.

Process.GetCurrentProcess()

Here is some code for you.

       Process[] pArry = Process.GetProcesses();
       foreach (Process p in pArry)
       {
           if (p.Id == Process.GetCurrentProcess().Id)
                continue;
           string ProcessName = p.ProcessName;
           if(ProcessName == Process.GetCurrentProcess().ProcessName)
           {
                MessageBox.Show(AppAlreadyRunning,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
                SetForegroundWindow(p.MainWindowHandle);
                Application.Exit();
           }
       }   
Craig White
  • 13,492
  • 4
  • 23
  • 36
  • 1
    @Swanand Purankar: although this does answer your question, you should really look at using a kernel mode construct to do this. Even if you are checking process names it's easy for a user to make multiple copies of your exe each with a different name. In that case, your process name check wouldn't stop the user from the running all the exe's simultaneously. – Adam Ralph May 13 '11 at 16:07
  • @AdamRalph Though I am replying almost after a Year & Half, I remember, I used Mutex too... Thanks to Your comment!! :) – Swanand Oct 27 '12 at 09:04
4

These questions seems quite similar...

https://stackoverflow.com/questions/93989/prevent-multiple-instances-of-a-given-app-in-net

https://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c/229567

Community
  • 1
  • 1
JeremyWeir
  • 24,118
  • 10
  • 92
  • 107
  • Thanks....Though I searched first, I couldn't find it... This thread is really helpful! – Swanand Apr 11 '11 at 06:18
  • +1 use of a kernel mode construct such as mutex is definitely the way to do this (although using a semaphore will have slightly less overhead). It's easy to change the process name of a second instance to avoid a check which relies on process names. – Adam Ralph May 13 '11 at 16:04
1

I bet you could use Process.GetCurrentProcess() (as you do to kill the process) to get the actual name and use that for comparison.

SirPentor
  • 1,994
  • 14
  • 24