Single instance Application in c#


Working one standalone Application I had one requirement like restrict the user for opening multiple instance of same Application.
 
Using the below approach we can successfully restrict Opening of multiple instance of the same application. .Net provide a class Process which is used for getting the information of all running process. It is available in System.Diagnostics namespace.

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
 
//Read the current Process
Process _trackerProcess = Process.GetCurrentProcess();
 
//Read all process by current process name.
Process[] _systemProcess = Process.GetProcessesByName(_trackerProcess.ProcessName);
 
if (_systemProcess.Where(x => x.Id != _trackerProcess.Id).Count() >= 1)
{
MessageBox.Show("Another instance running");
return;
}
Application.Run(new Tracker()); 

No comments:

Post a Comment