Application instance

  • Thread starter Thread starter T Cordon
  • Start date Start date
T

T Cordon

How to determine if another instance of the application is running so that
ir doesn't run multiple instances?


Thanks
 
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, or Tools->Create
GUID...
'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
 
The only sure way is to create a Mutex and try to acquire ownership of it
every time your app opens.

Luckily using a mutex from .NET is simple.
 
Hi,

Start your app from sub main use a mutex to see if there is another
instance running.

Public Sub main()

Dim owned As Boolean

Dim mut As New System.Threading.Mutex(True, "xvcjsdf67AS124#$3", owned)

If owned Then

Application.Run(New Form1)

mut.ReleaseMutex()

Else

MessageBox.Show("A previous instance is already running")

End If

End Sub



Ken
 
Windows forms Tips and Tricks has an example that also brings the running
instance to the foreground.

http://www.bobpowell.net/tipstricks.htm

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
Having detected another instance of the application is it possible to make that instance become the active application instead of
MessageBox.Show("A previous instance is already running")

Thanks
 
Um.... I might be missing something blindling obvious here... but when I
upgraded some VB6 code the other day, it changed my code to this;

Public Sub Main()
Try
' Disallow other copies running
If GetProcessesByName(GetCurrentProcess.ProcessName).GetUpperBound(0)
= 0 Then
Application.EnableVisualStyles()
Application.Run(New frmMain)
End If
Catch vException As Exception
MessageBox.Show(vException.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub

I take it this code neglects to let your application run if another process
with the same name also happens to be running... I suppose if your app is
called MyDatabaseAppOnMyPC.exe, the chances of a conflict are low.. lol
_______________________
The Grim Reaper
 
Curious. What did the VB 6 code look like before the upgrade?

Yes, simply checking for the same named process is not very full proof. I
am surprised the upgrade wizard would go that route. A mutex seems to be the
safer bet.

Greg
 
Oh yes, I agree the mutex thing looks a whole lot more solid!! The old VB6
code (I've found it in most of my old projects after a brief search - dunno
where I got it from) was something like this;

Sub Form_Load()
If App.PrevInstance Then
Msgbox "<app name> is already running!", vbOKOnly + vbInformation,
"Stupid User Alert"
Unload Me
End If
End Sub

Seems oh-so-simple looking back... lol.
____________________________
The Grim Reaper
 
Back
Top