Results 1 to 3 of 3

Thread: [RESOLVED] Starting program in already started instance

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Resolved [RESOLVED] Starting program in already started instance

    Hello everyone.

    I was wondering how to PROPERLY make a multi-tabbed or multi-window application that can handle file start requests.

    For example, on startup my "Desktop Application" runs and sits there waiting for a task, either through the main menu, or for a file to display in a window. Now the problem is simple, how can I make this already running program load the dropped file (dropped on the executable (!)), instead of starting a new instance.

    I understand I will have to check if the process is running, and if so, do some stuff with it. But the problem is passing the message to the active program.

    Previously I used text files with paths/arguments in them, but I don't like the idea of the OS/program checking every 500 ms if a file is created in the "load this" folder...

    Then I thought of sending a message using "Send Message", for example changing the text in a textbox and handle the "Text Changed" event, but is this reliable? Can any other program interfere with it?

    So, basically I need the proper way of doing all of this. For example, Microsoft Visual Studio does this nicely, and no lag whatsoever...

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Starting program in already started instance

    1) Make sure you are using the application framework (My Project -> Properties -> Application tab)

    2) Make sure "Make single instance application" is checked.

    3) Click "View Application Events" to generate the ApplicationEvents.vb file.

    4) In that generated file, select "(My Application Events)" from the left combo box, and then "StartupNextInstance" from the combo box on the right to generate a handler that will be fired in your first instance of the application when a second instance is attempted to be started. The StartupNextInstanceEventArgs has a "CommandLine" property - if you drag a file onto the executable, the first string in this property will be the filename of the dropped file.

    5) If you are making a multi-window single-instance app (i.e. each "new" instance gets its own window) you should go back to orject properties and set the "Shutdown mode" to be "When last form closes", otherwise closing the "first instance"'s form will exit the app closing all the "instances".

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Starting program in already started instance

    Ah, cool. Never knew this existed.

    Code:
    Namespace My
        Partial Friend Class MyApplication
            Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
                For Each cmd As String In e.CommandLine
                    MsgBox(cmd)
                Next
            End Sub
        End Class
    End Namespace
    Thanks Evil_Giraffe
    Last edited by bergerkiller; Mar 15th, 2011 at 07:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width