Jul
10
2009
There can be only one!
I had to make a slight change to one of my applications for a customer today, making it check if it is already running on the local machine and, if it is, abandon it’s attempt to start up another instance.
My initial thoughts were to examine the executing processes and check for the existence of the application in the list:
1 2 3 4 5 6 7 8 | string procName = Process.GetCurrentProcess().ProcessName;
Process[] processes= Process.GetProcessesByName(procName);
if (processes.Length == 1)
{
//run application
...
} |
Now, whilst this works, it is easily “beatable” by simply changing the executable’s name. Doing so will cause the method “GetProcessessByName()” to return only a single match, instead of the current application and any other duplicate instances.