2

Let's say I have an application written in C# called EquipCtrl.exe which runs as a local process on a PC to control a piece of equipment.

Obviously, I would wish to have only one instance of Equipctrl to run on each PC. If I had two equip to control per PC, I would then limit it to two instances per PC.

The way I did is was either one of 1. Process name. I name the process EqCtrl and at process start-up, it would count the number processes with the name "EqCtrl". 2. Execution name. At start-up count the number of processes with the execution name EquipCtrl.exe. 3. Registry record. 4. SQL Server database record.

To me, process name or execution name detection is the simplest and what I do most (if not all) of the time. But, they are susceptible to name clashing. Even if I go further to find out the execution path, the limit could be circumvented by copying the execution file to another folder.

What is the best way to perform execution limiting on .NET? Why? Is registry record the best way?

Blessed Geek
  • 21,058
  • 23
  • 106
  • 176

4 Answers4

7

Try a system-wide Semaphore: http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

Stu
  • 15,675
  • 4
  • 43
  • 74
  • +1 (if I could up-vote). I've used this in C applications, and it should work great in C#. There is a bit of work you need to do, like check to see if another process has already created the semaphore. If not, then attempt to create it. If it then already exists, you need to see if you can lock it. The lock needs to try to lock the semaphore with a timeout. If it times out, then your process is an extra one, and should exit. There is a bit of work to get all this right, but it is the correct answer to the question. – aaaa bbbb Nov 11 '10 at 22:15
  • And use a named semaphore, with all process instances using the same name. – aaaa bbbb Nov 11 '10 at 22:23
1

Scott Hanselman's Blog has a great post on this...

http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx

Chris Baxter
  • 16,083
  • 9
  • 51
  • 72
  • The article does not seem to address the occasional need to allow more than one instance of the program on one PC. – aaaa bbbb Nov 11 '10 at 22:21
  • That would depend on how you handled OnStartupNextInstance; the part I found interesting and relavent was that C# developers never consider using the tools available in the Microsoft.VisualBasic assembly because it is "bad"; however there are useful tools in there that can save development effort. – Chris Baxter Nov 11 '10 at 22:24
0

Hmm .. when we want to allow only a single instance of an application we use a named mutex

That doesn't exactly enable the scenario you desire.

Another point, to avoid collisions, in addition to the executable name and directory, use an MD5 hash of the executable file.

quentin-starin
  • 26,121
  • 7
  • 68
  • 86
0

You can do this with counting named mutex\semaphore, also make sure the mutex/semaphore prefix you create starts with GLOBAL\ and LOCAL\ otherwise remote desktop connection won't read these named mutex and count will be lost.

http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx

Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47