Important alert: (current site time 7/15/2013 7:43:43 AM EDT)
 

article

A great way (the only right way) to check for previous instances

Email
Submitted on: 12/1/2004 5:26:35 PM
By: Hans Christian Ihlen  
Level: Beginner
User Rating: By 2 Users
Compatibility: C#, VB.NET
Views: 11368
 
     This is a function that will check if a previous instance of your application is already running that are much faster and more reliable than the others I've tried.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Before I always used this code to check if a previous instance was already running, but it I wasn't happy with it as my floppy always started rumbling and it took some time to do the check. It even failed a few times.

bool AlreadyRunning = (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1);
 
This example is much faster and much more reliable:
C# code:

//Put the next line of code at the very top
using System.Runtime.InteropServices;

public
const int ERROR_ALREADY_EXISTS = 183;

[DllImport("Kernel32.dll", SetLastError =
true)]
public static extern int CreateMutexA(int lpSecurityAttributes, bool bInitialOwner, string lpName);

[DllImport("Kernel32.dll", SetLastError = true)]
public static extern int GetLastError();

public bool IsProcessAlreadyRunning()
{
   
//Attempt to create defualt mutex owned by process, MyApp can e.g. be the name of the executable
   
CreateMutexA(0, true, "Automove");
   
return (GetLastError() == ERROR_ALREADY_EXISTS);
}


VB.NET Code:

'Put the next line of code at the very top
Imports System.Runtime.InteropServices

Public
Const ERROR_ALREADY_EXISTS As Integer = 183

Public
Declare Function CreateMutexA Lib "Kernel32.dll" (ByVal lpSecurityAttributes As Integer, ByVal bInitialOwner As Boolean, ByVal lpName As String) As Integer

Public Declare Function GetLastError Lib "Kernel32.dll" () As Integer

Public Function IsProcessAlreadyRunning() As Boolean
   
'Attempt to create defualt mutex owned by process, MyApp can e.g. be the name of the executable
   
CreateMutexA(0, True, "MyApp")
    Return (GetLastError() = ERROR_ALREADY_EXISTS)
End Function

Hope this is of some help.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/13/2006 12:05:05 PMNDawg

The main problem is that you break any OS-independence the code previously had (by using P/Invoke). I mean, if you are going to use P/Invoke, why don't you just write in C++ to begin?
(If this comment was disrespectful, please report it.)

 
10/4/2006 11:01:40 AMD.Huyen

Student
(If this comment was disrespectful, please report it.)

 
5/2/2008 12:21:35 AMMike Morrow

Thanks for posting. In VB9 (2008), this feature is built in and does not need any user coding.
(If this comment was disrespectful, please report it.)

 
1/22/2010 1:31:25 AM

i just love you man!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.