Search code examples
c#.netsingle-instance

Correct .NET way to implement a single instance application


I have seen at least three distinct methods on StackOverflow for achieving this.

  1. Using a MUTEX: Accepted answer to this SO question

  2. Using the Microsoft.VisualBasic library's WindowsFormsApplicationBase: Second highest voted answer to this SO question

  3. Using Process.GetProcessNames to check if your application is running: Method here was posted as an answer to this SO question

I'm sure there are more ways to do this as well.

I'm simply wondering if one of these is preferred and what the consequences might be if I pick the "wrong" one.


Solution

  • When in doubt, always prefer an implementation that's included in the .NET framework. You can have high expectations that such an implementation is tested by hundreds of thousands of programmers, has been carefully reviewed for security and usability and will be maintained for years to come.

    The mutex approach is an easy one to get going. It however suffers from a pretty severe security problem. A denial of service attack is very simple to get going, you cannot keep the name of your mutex a secret and anybody can trivially create a mutex with the same name and prevent your program from ever starting up.

    The process name approach is deeply flawed for the same reason. There is no guarantee that a process name is unique. Not just easy to exploit but easily triggered by accident.

    Last but not least, it has a feature that's hard to implement yourself. The StartupNextInstance event is available to detect that the user started your app again. The default implementation for this event automatically activates the main window of the running instance, nice. Implement your own to parse command line arguments.

    WindowsFormsApplicationBase has an image problem in the eyes of C# programmers. They choke at the namespace name and assume that their program will somehow be infected with vb-isms. That's nonsense, it is just a plain .NET class that's useable in any language.