'vb 5/6 If App.PrevInstance Then MsgBox(“Program is already running.”) Exit Sub End If
Edit: In VB 2005 Microsoft added a “Make single instance application” feature under the Project; Propertys Menu on the Application Tab. Just check that checkbox to make your application allow only 1 instance. So the source code below is for Visual Basic.NET 2002 and VB.NET 2003 only.
This uses the Process class to check for the name of the current applications process and see whether or not there are ‘more than 1x instance loaded.
'The end result of this code is similar to Visual Basic 6.0's App.Previnstance feature. Dim appName As String = Process.GetCurrentProcess.ProcessName Dim sameProcessTotal As Integer = Process.GetProcessesByName(appName).Length If sameProcessTotal > 1 Then MessageBox.Show("A previous instance of this application is already open!", " App.PreInstance Detected!", MessageBoxButtons.OK, MessageBoxIcon.Information) Me.Close() End If appName = Nothing sameProcessTotal = Nothing