hrabia
Centurion
How to check that only one instance (pro user) of my application is running?
look into 'Mutex'hrabia said:How to check that only one instance (pro user) of my application is running?
'Checks to see if application is already running
Function PrevInstance() As Boolean
Try
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
Catch ex As Exception
End Try
End Function
Dim m As Threading.Mutex
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim b As Boolean
m = New Threading.Mutex(True, "Test", b)
If b = True Then
'already running
Else
'guess what - not running
End If
End Sub
PlausiblyDamp said:Checking for the process by name isn't an atomic operation and as such if two instances where launched very close together it is possible for the second to not detect the first.
Joe Mamma's suggestion of using a mutex is more reliable, quick sample
Visual Basic:Dim m As Threading.Mutex Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As Boolean m = New Threading.Mutex(True, "Test", b) If b = True Then 'already running Else 'guess what - not running End If End Sub
Dim m As Threading.Mutex
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim b As Boolean
m = New Threading.Mutex(True, "Local\\AppNameHere" , b)
If b = True Then
'already running
Else
'guess what - not running
End If
End Sub
Joe Mamma said:Is this for licensing purposes?
If the above is true, you need to look at ServicedComponent and the ObjectPoolingAttribute, couple this with .NET remoting.
This can get pretty complex. So if its not for licensing, I think an architecture redesign may be in order.
If it is for licensing, I will sell you a universal solution for US$995.
PlausiblyDamp said:"Local\\AppNameHere"
and it should work, not got a TS handy and the local citrix server is being rebuilt so I haven't had chance to test the above code.