Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ


Prevent Multiple Instances of a .NET Windows Application by PankajBanga
Posted: 21 Apr 04

Copy the following code in Module declaration of your application.

'Declarations of Windows API functions.
    Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

    Sub ActivatePrevInstance(ByVal argStrAppToFind As String)

        Dim PrevHndl As Long
        Dim result As Long

        'Variable to hold individual Process.
        Dim objProcess As New Process()

        'Collection of all the Processes running on local machine
        Dim objProcesses() As Process

        'Get all processes into the collection
        objProcesses = Process.GetProcesses()

        For Each objProcess In objProcesses
            'Check and exit if we have SMS running already
            If UCase(objProcess.MainWindowTitle) = UCase(argStrAppToFind) Then
                MessageBox.Show(argStrAppToFind & " is already running.", "System", MessageBoxButtons.OK, MessageBoxIcon.Information)
                PrevHndl = objProcess.MainWindowHandle.ToInt32()
                Exit For
            End If

        'If no previous instance found exit the application.
        If PrevHndl = 0 Then Exit Sub

        'If previous instance found.
        result = OpenIcon(PrevHndl) 'Restore the program.
        result = SetForegroundWindow(PrevHndl) 'Activate the application.

        'End the current instance of the application.

End Sub

Then, use the following code in Form Load Event of your Main (starting) Form.

'Prevent multiple instances of the application.
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
End If

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close