I thought that the "Application Framework" tickbox enabled a suite written by the VB guys that, among other things, causes the ORIGINAL app to receive an event when another instance is attempted. The command line parameters are passed and the original app can do whatever.
Essentially the first app starts a remoting server. Subsequent attempts fail to bind to the remoting port for that app because it is already in use, so the existing server is notifed of command line args etc.
I wrote an app to leverage this, here's the code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
using System.Drawing;
using System.Runtime.InteropServices;
namespace StartBay
{
static class Program
{
[DllImport("mpr.dll")] private static extern int WNetRestoreConnectionW(int phWnd, string psLocalDrive);
static MainForm mf;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
WNetRestoreConnectionW(0, null);
bool backgroundMode =
(args != null && args.Length == 1 && args[0].Equals("/background")) /*|| cmdLine.Contains("background=true")*/;
try {
mf = new MainForm();
FormAnimation.FormAnimator fa =
new FormAnimation.FormAnimator(mf, FormAnimation.FormAnimator.AnimationMethod.Blend, 250);
SingleInstanceApplication.Run(mf, [B]StartupNextInstanceHandler[/B], backgroundMode);
} catch(Exception e) {
if(!backgroundMode){
string targMsg = "removed";
MessageBox.Show("removed");
}
}
}
[B] static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)[/B]
[B] {[/B]
[B] e.BringToForeground = true;[/B]
[B] if(!mf.Visible)[/B]
[B] mf.Show();[/B]
[B] else[/B]
[B] mf.BringToFront();[/B]
[B] mf.ShowNIDAdviceBalloon();[/B]
[B] }[/B]
[B]}[/B]
public class SingleInstanceApplication :[B]WindowsFormsApplicationBase[/B]
{
private SingleInstanceApplication()
{
base.IsSingleInstance = true;
base.ShutdownStyle = ShutdownMode.AfterMainFormCloses;
base.EnableVisualStyles = true;
}
public static void Run(Form f, StartupNextInstanceEventHandler startupHandler, bool startHidden)
{
SingleInstanceApplication app = new SingleInstanceApplication();
[B]app.StartupNextInstance += startupHandler;[/B]
if(startHidden)
f.Location = new Point(-32000, -32000);
else {
Rectangle scrn = Screen.GetWorkingArea(f);
f.Location = new Point((scrn.Width - f.Width) / 2, (scrn.Height - f.Height) / 2);
}
app.MainForm = f;
app.Run(Environment.GetCommandLineArgs());
}
}
}
It's C# but the interesting parts for you are in bold. Your application (My.Application) should already be an instance of WindowsFormsApplicationBase.
This means if you add a handler to the My.Application.StartupNextInstance event then that event will fire when someone attempts to start a second instance
In my code here I handle this with the bold method
static void StartupNextInstanceHandler
In your code I guess it would be something like writing a method of the same signature that my
static void StartupNextInstanceHandler has, and then AddHandler-ing it to attach it to My.Application.StartupNextInstance
All I do with it are bring the existing instance to the forground and show it. Also, if the user started a second instance I popup a little ballon saying something like "It's faster to double click me in the tray, to activaste the xisting instance, than it is to start a new copy"
The StartupEventArgs object should have e.g. the command line args and remember this way it is the ORIGINAL app that experiences this event which gives you more control than the secondary app merely being able to activate..