Creating a single instance application
We may sometimes want to limit the number of running instances of some application to just one. Running some executable creates a new Windows process to host that executable, with its own address space, tables, resources, and so on. Sometimes this is not desirable; if the executable tries to run while another instance is already running, it should quit and optionally make the other instance's main window active. A canonical example in Windows is Windows Media Player. An attempt to open a second media player activates the first Media Player window (although it's debatable whether such behavior is desired for Media Player).
Let's see how this can be achieved.
Getting ready
Make sure Visual Studio is up and running.
How to do it...
We'll create a simple application that will not run with more than one process. A second run will transfer activation to the running instance's window, and quit:
Create a new WPF Application project named
CH05.SingleInstApp
.Open
MainWindow...