How can we make a single instance application in C#? It is easy with VB.Net,only a checkbox. But I couldn't find a HowTo for C#.
Printable View
How can we make a single instance application in C#? It is easy with VB.Net,only a checkbox. But I couldn't find a HowTo for C#.
At startup enumerate all available processes and check their name with the name of the currently running process. If equal (and the process id's differ) then it is already running and you could:
a) Shutdown and print an error message
b) Focus on the already running process
You mean there is no way to tell .net host to run only one instance of an application (using domains or something...)?
Use a named mutex. If you get it, your process is the first to run. If you fail to get it, there is already an instance running. It's way faster than scanning processes, plus you cannot manipulate it as easily as renaming a process.
Code:private static System.Threading.Mutex m_SingleInstanceMutex;
// in main:
m_SingleInstanceMutex = new Mutex( true, @"Local\YourName", out returnValue );
if( ! returnValue )
{
// instance of this executable already running
}