Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  How to detect if application is already running in .NET (Thursday, September 02, 2004)




Found the following interesting discussion in the Newsgroups:

Checking if running
by:MadCrazyNewbie

Hey Group,

How would I check if my Application is running on a local PC and if it is throw a message to say its already running and quit the the second instance?

Many Thanks
MCN


 Reply:
by:Cor Ligthert

 Hi MS,

This is the very simple solution from this.

\\Private mut As New Threading.Mutex(True, "myProg", mutCreated)
)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
///

But remember that when you use this, and there can be a complete other program with the same name as yours, that program can also not start.

Better
Single instance mostly C# code
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/reaworapps1.asp

I hope this helps?

Cor


 Reply:
by:hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP])

 

For example, in your 'Sub Main':

\\Dim m As Mutex = _
New Mutex(False, "{11C92606-65D9-4df2-9AEA-B6A4DA91BCE2}")
If m.WaitOne(10, False) Then
Application.Run(New Form1())
m.ReleaseMutex()
Else
MessageBox.Show("Anwendung wird bereits ausgeführt!")
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


 Reply:
by:Cor Ligthert

 Hi MS,

It is almost the same as the solution i did give you however this signature from your program is of course better, (Herfried, can that not direct?, you know that direct and I have to search how.)

However I think that this will give you on a lot of places in the world very much respect

:-))

Cor


 Reply:
by:hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP])

 
The difference is that it uses 'WaitOne' to avoid possible problems
which can occur on some machines (don't know why).

> However I think that this will give you on a lot of places in the world very
> much respect
>> MessageBox.Show("Anwendung wird bereits ausgeführt!")

Oh, sorry...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>








 
Previous Posts
    - GetForeGroundWindow
    - Reflection: Determining a Method's Attributes from...
    - Open a form from menu
    - Adding a reference in VS.NET
    - Converting String Value to a Formula for calculati...
    - Microsoft Office Document Image Writer & Vb.Net
    - AddRange method for CheckedListBox
    - Any way to extract certain bytes from the remote f...
    - Date format problem with vb.net
    - Dynamic Expression Evaluation

Archives
    - 08/01/2004 - 08/08/2004
    - 08/08/2004 - 08/15/2004
    - 08/15/2004 - 08/22/2004
    - 08/22/2004 - 08/29/2004
    - 08/29/2004 - 09/05/2004
    - 09/05/2004 - 09/12/2004
    - 09/12/2004 - 09/19/2004
    - 09/19/2004 - 09/26/2004
    - 09/26/2004 - 10/03/2004
    - 10/03/2004 - 10/10/2004
    - 01/02/2005 - 01/09/2005
    - 01/09/2005 - 01/16/2005
    - 01/30/2005 - 02/06/2005
    - 01/01/2006 - 01/08/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland