Project Description
This C# project helps in the creation of single instance applications (either per user session or per host) using WPF.
It does not use the Micrsoft.VisualBasic.WindowsFormApplicationBase approach, relying instead on the usage of synchronization objects and WCF.

Introduction
To create a single instance application, two prerequisites must be matched:
- The application should be aware of other instances of itself already running
- If another instance of the application is started, the main one (tipically the first one) should be notified and handle the situation instead

There are many ways to create an application matching such requisites, using WPF:
  1. Using Microsoft.VisualBasic.WindowsFormsApplicationBase (which matches both requisites) to create a manager to be attached to the application (examples can be found here, here, here, here and here)
  2. Using a synchronization object to check for already running instances, and a way to deal with inter-process communication (using Interop services and memory mapped files like here, or IsolatedStorageFile like here)
  3. Using WCF and named pipes to create a service and deal with both multiple instance identification and inter-process communication (using the source code provided here)
There is an even simpler solution: enumerating among ruinning process... unfortunally this approach is pretty weak (what if two different applications share the same name?) and does not deal with the inter-process communication issue. Moreover, it is so ugly that I don't even consider it a real solution! :)

The project
Since there are so many solutions, you could argue that there was no need for another one... true, but I like doing things my own way, and each of the previous solutions had something I didn't like: some of them required to get rid of the App.xaml file, others rely on an language specific assembly (Microsoft.VisualBasic) and feel like a hack (avoiding the WindowsFormApplicationBase message pump to start, using the object just to deal with instance check and notification), others do not take into account the possibility of having a single instance of the application, either in the user session or globally per host (moreover use method hiding, since the application Run method is not virtual).
So, the solution proposed in this project was mostly inspired by the approaches from Marco Zhou and John Melville: it uses a named synchronization object to deal with instance identification (this way it can be aware of both applications running in the same session or different ones), and WCF with named pipes to perform inter-process communication. It does not hide methods, using only method overriding (in particular OnStartup and OnExit), and provides a way to notify 'next' instances about main application receive of 'next instance startup' signal.
With this approach the default App.xaml and App.cs have to be slightly modified (like in the Marco Zhou solution), since InstanceAwareApplication inherits from System.Windows.Application, but there is no need for a manager or explicit Main method definition.

In a few words, it is a clean and simple solution, which tries to summarize the best from the solutions proposed until now.

Last edited Jul 7, 2010 at 3:03 PM by BladeWise, version 11