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. ...

Get Windows Presentation Foundation 4.5 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.