Question

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.

Was it helpful?

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.

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.

OTHER TIPS

Why nobody mentioned ticking this checkbox?

enter image description here

It's really a matter of taste, but I favor the Mutex approach, simply due to it not requiring a dependency on the VisualBasic libaries, and using Process.GetProcessNames is a non-ideal solution (as mentioned, process names aren't always going to map to what you think they might)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top