[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.
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.
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 ...
Re: Open multiple files without starting multiple instances (Application.CommandLineA
Quote:
Originally Posted by
paulg4ije
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.
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 :o
Many thanks to you both.