Can only open one instance of my program

hzeigler4

Member
Joined
Jun 19, 2006
Messages
13
Programming Experience
3-5
I have an exe installed that I created from my VB.NET app. Is there code I can put in that will only let a user open my program once and if they try to open it and it is already open, I want to set the focus to it. This way they wont be able to accidently open two instances of the program and be typing data into two different ones.
 
In VB2005 it's in the 'Application' tab of Project properties. It's convenient with only clicking one checkbox, but also remember there are several ways one perhaps want this to be done and the default one may not fit, so you perhaps have to write it yourself anyway.

Thanks for being helpful and change your profile to VB2003 to avoid such confusion :) You also then have to write some code to do this, the first 3 search engine hits were Code Project articles one, two and three. Looks like there are really a lot of articles, discussions and code examples on how to do this in earlier VB versions.
 
I tried to add the following code and for some reason it does not recognize the type Process. ex Dim Procs() As process

Imports System.Diagnostics
Module SingleInstance
'Entry point for the application. Main checks for a prior instance and closes
'this one if it finds a prior one running.
Public Sub Main()
If CheckForDuplicateProcess("ScannerAppCE") Then
Dim dupProcess As String
dupProcess = "There is another instance of ScannerAppCE" & _
" running on this machine." & vbCrLf & _
"This new instance must close." & vbCrLf & vbCrLf & _
"Only 1 instance of ScannerAppCE can exist" & _
" on a machine." & vbCrLf & vbCrLf
MsgBox(dupProcess, MsgBoxStyle.Critical, "Duplicate Process Detected")
Application.Exit()
Else
'****** Change The FormName Below *****
'Change the name of the form to load, to the one
'applicable for your application
Application.Run(New frmLocInvScan)
End If
End Sub
'Determines if there already is a 'processName' running in the local host.
'Returns true if it finds more than 'one processName' running
Private Function CheckForDuplicateProcess(ByVal processName As String) As Boolean
Dim Procs() As process = Process.GetProcesses()
Dim proc As Process
For Each proc In Procs
If proc.ProcessName.ToString.Equals(processName) Then
If proc.Id < Process.GetCurrentProcess().Id Then
Return True
End If
End If
Next proc
Return False
End Function


 
add this function to your startup form (or to the module that sub main resides in, if that's where it's starting from):
VB.NET:
Function PrevInstance() As Boolean
  If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
    Return True
  Else
    Return False
  End If
End Function
then to use it:
VB.NET:
'From a form:
If PrevInstance() = True Then Me.Close()

'From sub Main
Public Sub main()
  If PrevInstance() = False Then
    'the rest of your sub main here
  End If
End Sub
 
Diagnostics resides in the System namespace (which is normally imported by default) so just change it to System.Diagnostics.... and there ya go
VB.NET:
Function PrevInstance() As Boolean
  If Ubound(System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
    Return True
  Else
    Return False
  End If
End Function
 
Are you sure you can do this is 2003? I added this at the top:

Imports System.Diagnostics

This is fine and has no errors.

but System.Diagnostics.Process is not available??
 
'Process' is not a namespace, it is a class, so it can't be imported. It does however expose shared methods. So to use it you would type..

System.Diagnostics.Process.Start

Directly in the method. No importing necessary.
 
OK I am not stating this clearly. I added the function:

'hdt
Function PrevInstance() As Boolean
If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function

The word process has the blue squiggly line under it. When I hold the cursor over the word process it says "Process is not a member of diagnostics"

I am using vb.net 2003
 
Back
Top