Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct .NET way to implement a single instance application [closed]

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.

like image 601
Michael Mankus Avatar asked Dec 31 '12 18:12

Michael Mankus


People also ask

How do I run a single instance of the application using Windows Forms?

" This feature is already built in to Windows Forms. Just go to the project properties and click the "Single Instance Application" checkbox. "

What is single instance application?

A Single Instance application is an application that limits the program to run only one instance at a time. This means that you cannot open the same program twice.


2 Answers

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.

like image 136
Hans Passant Avatar answered Sep 30 '22 11:09

Hans Passant


Why nobody mentioned ticking this checkbox?

enter image description here

like image 27
Neolisk Avatar answered Sep 30 '22 11:09

Neolisk