Ok, here is some sample code:
Running
Your main class needs to extend WindowsFormsApplicationBase class, and you need to call Run(); method of it in Main() function:
////// We inherit from the VB.NET WindowsFormApplicationBase class, which has the /// single-instance functionality. /// class App : WindowsFormsApplicationBase { public App() { // Make this a single-instance application this.IsSingleInstance = true; this.EnableVisualStyles = true; // There are some other things available in the VB application model, for // instance the shutdown style: this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses; // Add StartupNextInstance handler this.StartupNextInstance += new StartupNextInstanceEventHandler(this.SIApp_StartupNextInstance); } static void Main(string[] args) { App myApp = new App(); myApp.Run(args); }
Now, we need to override OnCreateMainForm() function to create our MainForm:
protected override void OnCreateMainForm() { // Create an instance of the main form and set it in the application; // but don't try to run it. this.MainForm = new MainForm(); // We want to pass along the command-line arguments to this first instance // Allocate room in our string array ((MainForm)this.MainForm).Args = new string[this.CommandLineArgs.Count]; // And copy the arguments over to our form this.CommandLineArgs.CopyTo(((MainForm)this.MainForm).Args, 0); }
Last thing is setting application to pass command line arguments to our form when second instance is run:
protected void SIApp_StartupNextInstance(object sender, StartupNextInstanceEventArgs eventArgs) { // Copy the arguments to a string array string[] args = new string[eventArgs.CommandLine.Count]; eventArgs.CommandLine.CopyTo(args, 0); // Create an argument array for the Invoke method object[] parameters = new object[2]; parameters[0] = this.MainForm; parameters[1] = args; // Need to use invoke to b/c this is being called from another thread. this.MainForm.Invoke(new MainForm.ProcessParametersDelegate( ((MainForm)this.MainForm).ProcessParameters), parameters ); }Of course, you need to have proper delegate type in your form declared, and implemented target function for processing parameters:
public delegate void ProcessParametersDelegate(object sender, string[] args); public void ProcessParameters(object sender, string[] args){ /* ... */ }
No comments:
Post a Comment