App.Previnstance Equivalent using VB.NET 2003 and Earlier

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

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

Leave a Reply

Your email address will not be published.