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

2 thoughts on “How to make a single instance application in VB.NET 2003 and Older

  1. saleem

    i am using visual basic .net2005
    please can you tell me how to get all of the file names i selected and pressed enter to play them with my application
    i tried command$ and commandlineargs
    but this give you just on file name
    because every file sent to an instance of your application
    i tried startupnextinstance
    i got just on filename with every instance
    but this is very slow way to get all file names
    you have to wait all files to be sent
    this mean 100 files=100 instance and long time
    and above, this give aproblem with application
    a messsege is shown(the application has aproblem and has to close)
    i want away to get all of the files i selected and pressed enter to play with my application
    thgether and in just one time
    i think i have to use windows api

    but i dont know how
    and thank you…

    Reply

Leave a Reply