Kill All Other Instances - There can only be one!


Here is one method of making sure there is only one instance of your app running. In this example, the new app closes all the others. The code can be modified for the app to close itself if there is another instance running.



private static void KillAllOtherInstances()
{
    Process curr = Process.GetCurrentProcess();
    Process[] procs = Process.GetProcessesByName(curr.ProcessName);
    foreach (Process process in procs)
    {
        if ((process.Id != curr.Id) && (process.MainModule.FileName == curr.MainModule.FileName))
        {
            process.Kill();
        }
    }
}

Comments