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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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.
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.
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.)
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.)