I have a windows application (C#) and i need to configure it to run one instance from the application at the time , It means that one user clicked the .exe file and the appl
We had exactly the same problem. We tried the process approach, but this fails, if the user has no right to read information about other processes, i.e. non-admins. So we implemented the solution below.
Basically we try to open a file for exclusive reading. If this fails (because anohter instance has already done this), we get an exception and can quit the app.
bool haveLock = false;
try
{
lockStream = new System.IO.FileStream(pathToTempFile,System.IO.FileMode.Create,System.IO.FileAccess.ReadWrite,System.IO.FileShare.None);
haveLock = true;
}
catch(Exception)
{
System.Console.WriteLine("Failed to acquire lock. ");
}
if(!haveLock)
{
Inka.Controls.Dialoge.InkaInfoBox diag = new Inka.Controls.Dialoge.InkaInfoBox("App has been started already");
diag.Size = new Size(diag.Size.Width + 40, diag.Size.Height + 20);
diag.ShowDialog();
Application.Exit();
}