Single instance wpf app

Topics: Bootstrappers & IoC, Getting Started
Nov 19, 2013 at 10:37 PM

What would be the best way to create a single instance wpf app using caliburn.micro? The requirements are pretty simple, I'm using the latest version of both WPF, .Net and Caliburn and the application must only allow one instance of itself running per user session.

So far I've used the strategy described on this StackExchange answer ( but I have no idea how to accomplish that using the bootstrapper from caliburn.

Any help would be appreciated, thanks!
Nov 20, 2013 at 8:27 AM
I use this method that I developed myself.
I use it in many applications, since it integrates seamlessly with the WPF environment, and allows for inter-process communication (notification of next instances startup). If next instances have to close (single instance application), you just need to handle the Started event, checking if the the instance is not the first one, and shutdown the next application.

With this approach, you just need to use an Application-derived object (just changing the App.xaml and App.cs), without the need of defining the Main method explicitly.
Depending on your needs, you can decide to integrate the next-instance check inside the Bootstrapper startup, or before... it just depends on this question: do I need to configure a bootstrapper when the next application has to close? Personally, I prefer to let the bootstrapper be configured, so that I can log next instances and eventually handle/report unexpected exceptions in the same way as the main application. But it is just a personal choice.

I am considering creating a NuGet package for the project, and integrate a Caliburn.Micro based sample.
Nov 20, 2013 at 10:19 AM
Thanks for the info! What I really miss is an example of how the bootstrapper and the single instance detection code should be used together. I mean, should I handle single instance detection and then run the bootstrapper or should I use the bootstrapper to handle single instance detection?
Nov 20, 2013 at 10:28 AM
As I was saying before: it depends on you. If you want/need to use stuff provided by the bootstrapper, the IoC or CM in general, the logic should be in the Bootstrapper (using the code I was speaking above, in the OnStartup override of the bootstrapper). Otherwise, you can override the App.OnStartup method, and procede to the Shutdown if the application is not the first one.

I prefer to handle it in the bootstrapper, since in my opinion there could be actions to be taken requiring bootrstrapper-provided objects (e.g. logging, message boxes). This approach comes to a cost: full initialization of the bootstrapper is performed. The cost, in my opinion, is negligible in respect to the benefits.
Nov 21, 2013 at 12:55 AM
I guess you are right, putting the logic into the bootstrapper makes more sense and the cost is negligible indeed.

I tried using your code but unfortunately I couldn't get it to work with Caliburn.Micro. I inherited from InstanceAwareApplication, created my unique fixed app GUID and subscribed to the necessary but nothing happened. And by nothing happened I mean that more than one instance of the application could be opened without problems.

Do I have to do anything different because I'm using the Caliburn bootstrapper? I'm using the latest release from nuget, the one with a sample bootstrapper.

On a side note, how hard would it be to accept command line args and pass them to the already running instance? I'm currently doing this manually but it would be nice to implement this together with the single instance behaviour.
Nov 21, 2013 at 2:39 PM
The code already deals with passing arguments (check the StartupNextInstance event). You can hook the event in the bootstrapper and process the arguments of the other application instance.

Regarding the shutdown, you need to check if the started up instance is the first one (App.Current.IsFirstInstance), then call Shutdown directly if the property returns false. The code does not assume that other instances have to be shutdown (that's why it is called InstanceAwareApplication and not SingleInstanceApplication ;) ).
Nov 21, 2013 at 4:20 PM
Check the latest source code from the project I posted above. I added a CM sample.
Jan 16, 2014 at 7:19 PM
Edited Jan 16, 2014 at 9:30 PM
BladeWise wrote:
Check the latest source code from the project I posted above. I added a CM sample.
I've added you solution to my CM project and can display the arguments passed from a second instance. I have a method in my ShellViewModel to handle the arguments. How do I get a reference to my ShellViewModel object to be able to use that method?

I solved the issue. Thanks for the project - it does exactly what I needed.