I am developing a program from which only one instance can work. I know that I can use mutex to prevent multiple instances from starting.
But I want that if a new instance of the application starts, it must interrupt the old one. The executable will always have the same name.
I tried to run the following in the form load event;
Process[] pname = Process.GetProcessesByName(AppDomain.CurrentDomain.FriendlyName.Remove(AppDomain.CurrentDomain.FriendlyName.Length - 4));
if (pname.Length > 1)
{
pname[0].Kill();
}
What actually works ..... once every blue moon. Seriously, this works .. the first time, the second time the application just does not load. If I run it another 5 times, it may work.
It does not seem too reliable, maybe someone has a more elegant solution?
Thank!
source
share