2 instances of the program running

  • Thread starter Thread starter Chris Thunell
  • Start date Start date
C

Chris Thunell

I have created a VB.net windows forms application. If I have the program
running and then I mistakenly click on my desktop icon again, I get a second
instance of the program running. Is there anyway to check / prevent a
second instance to startup and just take me to the already running
application. (I have seen applications in the past do this... double
clicking on the desktop icon just brings up the already running minimized
application.)

Any help would be greatly appreciated!
Chris
(e-mail address removed)
 
Chris Thunell said:
I have created a VB.net windows forms application. If I have the program
running and then I mistakenly click on my desktop icon again, I get a second
instance of the program running. Is there anyway to check / prevent a
second instance to startup and just take me to the already running
application. (I have seen applications in the past do this... double
clicking on the desktop icon just brings up the already running minimized
application.)

See http://www.pobox.com/~skeet/csharp/faq/#one.application.instance
 
If you do a search for PrevInstance in VS/VB you will get the following code
block

Function PrevInstance() As Boolean
If
Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName))
Return True
Else
Return False
End If
End Function

Just call it from Sub Main()

Sub Main
If Not PrevInstance Then
Dim MainForm as new Form1 '(or whatever)
Application.Run(MainForm)
End if
End Sub
 
Hi Chris,

This is in my opinion the most simple solution in VB

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()

However this goes wrong when there is another program with the same name.

When that can be a problem than you can look to the page from Jon or to a
page from Herfried, the page from Herfried is in German. However you can
take the code.

<URL:http://dotnet.mvps.org/dotnet/code/application/>
-> "Nur eine Instanz der Anwendung zulassen"

I hope this helps,

Cor
 
Jared said:
If you do a search for PrevInstance in VS/VB you will get the following code
block

Function PrevInstance() As Boolean
If
Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.Get
CurrentProcess.ProcessName))
Return True
Else
Return False
End If
End Function

That's only useful if you know there won't be any other processes with
the same name, however. You can get a much better guarantee of
uniqueness using mutexes.
 
This is the 3rd time in as many days I've posted this bit of code that I
use. I find searching Google groups very effective way to find information.
<wink><wink>

Since, nobody has commented on it, I am beginning to feel self concious. :)
I am curious if you guys wouldn't recommend this approach yourself? This was
code posted by another user quite sometime ago and seems to work as I
expect.

Greg


Option Strict On
Imports System.Threading

Module Main
Private Sub SubMain()

SingletonApp.Run(New MyForm)

End Sub
Enc Module


Public Class SingletonApp

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form)
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean

' use this to create a unique guid for your app...
'Dim g As New Guid
'g = Guid.NewGuid
'Debug.WriteLine(g.ToString)

m_Mutex = New Mutex(False, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4") '
arbitrary GUID
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function

Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex()
m_Mutex.Close()
End Sub
End Class
 
Hi Greg,

I thought that this was the code as Jon and Herfried have on there pages,
while Tom Shelton has showed it basicly as well what is presented on the
page of Herfried.

Therefore you get probably no comment.

If it is totally others than reply

I am always curious?

Cor
 
Back
Top