Preventing Multiple Instances of a VB.NET Application

I found this in the MSDN.
Visual Basic:
' Visual Basic .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
 
Great! Thanks .. I did search the Microsoft Site, but I didn't come up with anything (just the VB6 one I posted above) .. Is there a way to have it set the focus to the already existing instance?

M.
 
This returns the correct string as expected..
Visual Basic:
Diagnostics.Process.GetCurrentProcess.ProcessName
but the rest does not.
 
You can get the handle of the application's main window by using
the .MainWindowHandle property of the process you found.
I don't know, but I assume the first one started will be 0 in the
array of returned processes.
 
The following code will recognize when a multiple instance of an application has been run:

Visual Basic:
' Prevent Multiple Instances of the Application
Dim appProcesses As Process() = _
   Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)

If appProcesses.Length > 1 Then
   ' Terminate This Instance
End If
This code is placed on the main form and after MyBase.New() and before the InitializeComponent() ...

However, this leads to my next question ... I can't terminate the instance with a simple Me.Close() :confused: It just continues to execute, the code works because I've put a message box with the info in it and it works ... Is there another way I can terminate this instance?

Thanks!
M.
 
That's a new command I didn't know Derek, but still doesn't work .. here is the code that I have on my form ... I tried moving the "IF ... THEN" after the SplashScreen.Dispose() but that didn't work either (I thought maybe it would have to go after the initialization was done) .. the msgbox appears so I know it's being raised, but can't kill the darn app! Am I going to have to code it into the FormLoad event?

Thanks again! :-\
M.


Visual Basic:
Public Sub New()
   MyBase.New()

   '[MP]   Prevent Multiple Instances of the Application
   Dim appProcesses As Process() = _
      Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)

   If appProcesses.Length > 1 Then
      MsgBox("Application.Exit Triggered")
      Application.Exit()
   End If

   '[MP]   Show Splash screen while the Application Loads
   Dim SplashScreen As New frmSplashScreen()
   SplashScreen.ShowDialog()
   SplashScreen.Update()

   '[MP]   Initialize the Application (Global Settings)
   Initialize(Me)

   'This call is required by the Windows Form Designer.
   InitializeComponent()

   'Add any initialization after the InitializeComponent() call
    SplashScreen.Dispose()

    End Sub
 
Yes, which is what is strange .. I get the msgbox which indicates that it's a second instance and should close .. yet, both Me.Close() and Application.Exit() both don't kill the app .. it runs as normal!?!

Very strange ..

M.
 
You could make a Sub Main to initialize your program with. For
example, set your startup object to Sub Main from the project
properties, and add this class:
Visual Basic:
Public Class Init
    Public Shared Sub Main()
        If findPreviousInstance() Then
            Application.Exit()
        Else
            Dim frm As New Form1()

            Application.Run(frm)
        End If
    End Sub

    Public Shared Function findPreviousInstance() As Boolean
        'do your check
    End Function
End Class
 
I still can't get this to work... the value of appProcesses is always 0
Visual Basic:
Public Sub New()
   MyBase.New()

   Dim appProcesses As Process() = Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)

   If appProcesses.Length > 1 Then
      MsgBox("Triggered")
   End If
   InitializeComponent()
End Sub
 
Hey Robby,

Create a class module named "Initialize" and replace all code with the following:

Visual Basic:
Option Explicit On 


Public Class Initialize

   Shared Sub Main()

   '[MP]   Prevent Multiple Instances of the Application
   Dim appProcesses As Process() = _
   Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)
   If appProcesses.Length > 1 Then
      Application.Exit()
   Else
      Dim Main As New frmMain()
      Application.Run(Main)
   End If

End Sub

Set your startup project as Sub Main() and replace 'frmMain' with whatever your main form is called.

This works .. I'm running it without problems! If you still have problems, then I'll post a sample app and you can try that ...

M.
 
Back
Top