How do I make sure only one instance of an application is running

by 13. June 2008 14:59

I have tried various ways to solve this quite simple problem. I tried with something called Mutex.Name and it made the application I am working on really slow at startup which for me is not an option. So I looked around for a better solution. I also wanted to make sure only one instance of “calc” (calc.exe) is running for each user running the application through terminal services. First of all I’ll show you how you kill the other processes if any. The key here is to make sure the application is started from the same location as the current running instance.

public static void MakeSureOnlyOneInstanceIsRunning()
{
	Process current = Process.GetCurrentProcess();
	Process[] processes = Process.GetProcessesByName(current.ProcessName);
	Assembly assembly = Assembly.GetExecutingAssembly();
	foreach (Process process in processes)    
	{            
		if (process.Id != current.Id)        
		{            
			if (assembly.Location.Replace("/", "\\") == current.MainModule.FileName)
			{      
				process.Kill();            
			}
		}    
	}
}

Now all you have to do is call this little beauty at application start.

static void Main()
{    
	MakeSureOnlyOneInstanceIsRunning();        
	Application.EnableVisualStyles();    
	Application.SetCompatibleTextRenderingDefault(false);    
	Application.Run(new frmMain());
}

This is a real blessing for me. When I started working for this employer we had customers having to restart their servers because running low on memory. Record was 50 running instances on 2 users. That’s nice but I promised to tell you how you make sure only one instance of for example windows calculator is running when started from your application. It’s similar but different (like Douglas Adams probably would have said)! I did not notice this at first since we only have 5 users running the application remotely at the same time. How I noticed it is quite funny though. I got a phone call from one of our customers saying there was something wrong with the calculator. It would start and then close in the middle of them calculating things. Reason was I closed ALL running instances of the calculator. This one time by coincidence someone else started a calculator and the other persons calculator got killed. -- “Oh my god they killed calculator!!!”

This is how I solved it:

/// <summary>
/// Starts the calculator.
/// </summary>
private static void StartCalculator()
{    
	// first get the session of the currently connected user    
	int sessionID = Process.GetCurrentProcess().SessionId;        
	// second loop through all running processes     
	foreach (Process p in Process.GetProcesses())    
	{        
		// third if the proccess is named calc AND         
		// the sessionid is the same as the sessionid of the         
		// currently connceted user then kill it        
		if (p.ProcessName == "calc" && p.SessionId == sessionID)  
		{          
			p.Kill();  
		}  
	}     
	// fourth start a new calculator    
	Process proc = new Process();    
	proc.StartInfo.FileName = "calc";    
	proc.Start();
}

Since the application is a windows 2003 version of remote application (windows 2008) they could lose focus of the calculator. And because of not being able to alt+tab between applications It’s better to just kill the old one and start a new. Have fun getting control of your processes!

Tags:

Windows Forms

blog comments powered by Disqus

About the author

Life architect specialized in programming

Month List

Widget Twitter not found.

Root element is missing.X