Tag Archives: single instance application

How to make a single instance application in VB.NET 2003 and Older

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3.00 out of 5)
 

Visual Basic.NET 02/03 didn’t keep the Instance features that VB 6.0 had which was App.Previnstance. Not sure why they didn’t add a simple feature like that, but they didn’t. That simple code lets you know if your app is already open and then you can decide on what to do about the app.Preinstance results. So, for VB.NET 2002/2003 we have to be a little intuitive. Its really not that hard though once you understand the concept. Below is some simple code that will do this for you. Its taken from my code snippets page by the way. 🙂

Update: 2010, February 19th: Maxim mentioned about a different solution to this problem. Its more thorough and elegant. Check out this link to see his solution…



    Private Function preInstanceCheck() As Boolean
        '
        '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 the total is higher than 1 there is of course a duplicate instance already running.
        If sameProcessTotal > 1 Then

            MessageBox.Show("A previous instance of this application is already open!", _
                " App.PreInstance Detected!", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
            '
            'A duplicate instance is found. You can close the application or
            'whatever it is you want to do.
            'Me.Close()

            Return True

        Else

            Return False

        End If

    End Function

Revisited: 2015