If I understood correctly, you want to limit the execution of the application to one single instance.
1) If you want to prevent running a second one you can use this:
static void Main()
{
string mutex_id = "__SincleInstanceApp";
using (NamedMutex mutex = new NamedMutex(false, mutex_id))
{
if (!mutex.WaitOne(0, false))
{
return;
}
}
}
See also:
Single Instance Application in C#[
^]
2) If you want to end the already running instance (might be the wrong approach), you have to search for the other instance, and
kill[
^] it like this:
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
process.Kill();
}
}
Please note, that simply killing an other process hold a lot of possible issues, it should be used only if it is not responding. Better trying to Close it with process.
CloseMainWindow(), wait a while and only than killing it. But you also could communicate with your other application instance using an other mutex for example.