View Single Post
  #3  
Old 05-24-2005, 15:22
surferxyz surferxyz is offline
Friend
 
Join Date: Jan 2005
Location: Planet Earth
Posts: 77
Rept. Given: 0
Rept. Rcvd 9 Times in 4 Posts
Thanks Given: 12
Thanks Rcvd at 54 Times in 21 Posts
surferxyz Reputation: 9
VB.Net

Code:
            '#Zone " Prevent multiple instances "
            If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                'Send opening form's TEXT property as a parameter to the function "ActivatePrevInstance"
                Dim PrevHndl As Long
                Dim result As Long
                Dim argStrAppToFind As String = "XXXXXXXXXXXXXXXX"
                '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 objProcess.MainWindowTitle.Length > 9 Or objProcess.MainWindowTitle = "xxxxxxxxxxxxxxxxxxxx" Then

                        If objProcess.MainWindowTitle = "xxxxxxxxxxxxxxxxxxxxxx" Then
                            MsgBox("Another instance of " & argStrAppToFind & " is already running on this machine. You cannot run TWO instances at a time. Please use the other instance.", MsgBoxStyle.Exclamation)
                            PrevHndl = objProcess.MainWindowHandle.ToInt32()
                            Exit For
                        End If
                        If (UCase(objProcess.MainWindowTitle.Substring(0, 12)) = UCase(argStrAppToFind)) Then
                            MsgBox("Another instance of " & argStrAppToFind & " is already running on this machine. You cannot run TWO instances at a time. Please use the other instance.", MsgBoxStyle.Exclamation)
                            PrevHndl = objProcess.MainWindowHandle.ToInt32()
                            Exit For
                        End If

                    End If
                Next
                If PrevHndl = 0 Then Exit Sub 'if No previous instance found exit the application.
                ''If found
                result = OpenIcon(PrevHndl) 'Restore the program.
                result = SetForegroundWindow(PrevHndl) 'Activate the application.

                End 'End the current instance of the application.
            End If
            '#End Zone
Reply With Quote