Thursday, October 4, 2012

[C#] How to run single instance application and to allow other instances to notify them

Today I wrote simple timer module for 3rd party application. Problem was that 3rd party application allowed only running other executables and, optionally, passing it command line arguments. I've designed simple timer interface in C# and I got an idea that I would allow only one application instance (I only need one timer) and for controls (start/stop) I will use other instances which will notify running instance before they close itself. I've found first solution on CodeProject that worked very well, but for some reason it kept failing on Marshal.Copy call due AccessViolationException. I didn't had time to check why is that, so I've grabbed next snippet of code, and it worked! This solution is using Microsoft.VisualBasic .NET library and WindowsFormsApplicationBase class. Apparently, this approach is using Mutexes and TCP channels internally to implement this, so if you are interested, there is a link on the CodeProject article which explains this (you should read this, btw).

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