Monday, March 09, 2009
In some design scenarios launching multiple copies of the same WPF application is a problem, especially for document-based applications or server applications.
WPF does not provide a native solution for single instance applications; however there are several workarounds for this issue.
The most commonly used solution is to check whether another instance of the application is already running when the Application.Startup event fires. This is easily done using a system wide mutex (provided by the operating system to allow interprocess communication). While this is easily done, it limits the developer’s options by not providing a way for the new instance to communicate with the already running instance. Mainly this will provide only a simple way of limiting the number of running instances to one, while a separate system will be required to handle the new calls (usually through remoting or Windows Communication Foundation).
The recommended and more useful approach is to use the built-in support that’s provided in Windows Forms and originally intended for Visual Basic applications. This approach handles the messy plumbing behind the scenes. This means using an old style application class as wrapper for the WPF application. The wrapper will handle the instance management and will communicate the request to the already running instance of the WPF application.
1) Add a reference to the Microsoft.VisualBasic.dll assembly.
2) Add a new custom class derived from the Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase class.
3) The IsSingleInstance must be set to true in the constructor. This enables a single instance application.
4) Override the OnStartup() method to create the WPF application object. (Note: The OnStartup() method is triggered when the application starts)
5) Overide the OnStartupNextInstance() method to handle future instances. (Note: The OnStartupNextInstance() method is triggered in when another instance of the application starts up)
6) Define a Main entry point for the application and create the wrapper object
7) Create the WPF application class
Sample of the application wrapper:
As you can see, the sample above contains the Main entry point for the application. This is required because the wrapper must be created first.
If Visual Studio is used, by default the App.xaml application definition style is used. This will not work with the wrapper because the App.xaml approach already has a Main entry point.
Remove App.xaml and App.xaml.cs from the project and create a new class for the entry point.
The only thing left to do is to create the WPF application definition:
Because the wrapper approach does not contain a XAML application definition (App.xaml), if you need to load application level resources, the following code can be placed the OnStartup() of the WPF application definition to load the resources from a resource dictionary:
The above code will work for dynamic resource definitions. The static resource lookup process will fail because the source for the application resources dictionary need to be specified. In that case the code to load the resources will be replaced with the following:
Note: AssemblyName is a placeholder for the actual assembly name. The Uri for the resource dictionary location can be changed as required.