Results 1 to 5 of 5

Thread: [RESOLVED] Open multiple files without starting multiple instances (Application.CommandLineArgs)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Resolved [RESOLVED] Open multiple files without starting multiple instances (Application.CommandLineArgs)

    I have a program that handles a particular (custom) filetype and I can start my program by double-clicking on an associated file. I use My.Application.CommandLineArgs to get the name of the file that was clicked and this is then loaded. If the user clicks on a file when the program is already running, a second instance of the app is started and the second file is loaded. I would like just one instance of the app to run and for multiple files to be loaded into it. I can detect if my app is running with something like Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName) but I don't seem able to retrieve the name of the file that was clicked under those circumstances. Any pointers towards getting the name of the file that was clicked when my app is already running?

    Thanks.
    Last edited by paulg4ije; Mar 31st, 2013 at 03:49 PM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Open multiple files without starting multiple instances (Application.CommandLineA

    This functionality is built into the application framework. You need to check off single instance application in the application properties page.

    Then you can click the View Application Events button and handle the StartupNextInstance event, which will pass in the file name via e.commandline so you can load the file into the first instance.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Open multiple files without starting multiple instances (Application.CommandLineA

    Many thanks for that. I had never noticed the "Single Instance" option. Very handy! I am getting an error when trying to handle the MyApplication_StartupNextInstance event, but I need to look that up to find the right way to extract the e.commandline parameters.

    Thanks again for your help.

    EDIT: Fixed the error. Just need to work out where to put my code that loads the file that has been clicked. A public sub on my main form doesn't work - ApplicationEvents.vb can't "see" it there ...
    Last edited by paulg4ije; Mar 31st, 2013 at 04:32 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Open multiple files without starting multiple instances (Application.CommandLineA

    Quote Originally Posted by paulg4ije View Post
    Just need to work out where to put my code that loads the file that has been clicked. A public sub on my main form doesn't work - ApplicationEvents.vb can't "see" it there ...
    If your main form is your startup form then yes it can. For a start, the startup form is the default instance of its type, therefore you can access it as you do any other default instance, i.e. via the form name. If Form1 is your startup form then that would be just:
    Code:
    Form1.SomeMethod(someValue)
    Secondly, the MyApplication class has a property that returns a reference to the main form. It is type Form though, because it can refer to any type of form. As such, you need to cast it as its actual type to access any members of that type, e.g.
    Code:
    DirectCast(Me.MainForm, Form1).SomeMethod(someValue)
    That last code is valid in the StartupNextInstance event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Open multiple files without starting multiple instances (Application.CommandLineA

    All OK now - rookie error! I forgot to prefix my method with the name of the form

    Many thanks to you both.

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