Preventing Multiple Instances of a VB.NET Application

I found this in the MSDN.
Code:
 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 didnt 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..
Code:
Diagnostics.Process.GetCurrentProcess.ProcessName
but the rest does not.
 
You can get the handle of the applications main window by using
the .MainWindowHandle property of the process you found.
I dont 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:

[VB]
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
[/VB]
This code is placed on the main form and after MyBase.New() and before the InitializeComponent() ...

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

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

Thanks again! :-\
M.


[VB]
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
[/VB]
 
Yes, which is what is strange .. I get the msgbox which indicates that its a second instance and should close .. yet, both Me.Close() and Application.Exit() both dont 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:
Code:
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 cant get this to work... the value of appProcesses is always 0
Code:
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:

[VB]
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
[/VB]

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

This works .. Im running it without problems! If you still have problems, then Ill post a sample app and you can try that ...

M.
 
Back
Top