Single Instance of Application

neeraja

New member
Joined
Jun 15, 2005
Messages
4
Programming Experience
3-5
Single Instance of Application - Resolved

Hi,

I have a requirement where I have to check if an instance of my VB.Net application is already running, If not then I need to load the application.

Also, when a menu option is clicked, the application has to check if the form is already open, if it is open, then the focus has to be brought to that form, else the form has to be opened.

Thanks
Neeraja
 
Last edited:
I use this to see if the app is already running:

VB.NET:
 Private Function PrevInstance() As Boolean 
 
Dim c As Integer
 
Dim pr As Diagnostics.Process
 
Try
 
c = UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName))
 
If c > 0 Then
 
Dim FoundSame As Boolean = False
 
For Each pr In Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
 
If pr.MainModule.FileName = Application.ExecutablePath And pr.Id <> Diagnostics.Process.GetCurrentProcess.Id Then
 
If pr.Responding Then
 
AppActivate(pr.Id)
 
FoundSame = True
 
Exit For
 
Else
 
pr.Kill()
 
End If
 
End If
 
Next
 
Return FoundSame
 
Else
 
Return False
 
End If
 
Catch ex As Security.SecurityException
 
Return False
 
End Try
 
Thanks, I have used the following code.



Private
Function CheckInstance() As Boolean
CheckInstance = False

Dim p() As Process

p = Process.GetProcessesByName(Application.ProductName)

If p.Length > 1 Then

CheckInstance = False

Else

CheckInstance = True

End If

Return CheckInstance

End Function

 
Back
Top