I deployed my C# WinForms application using ClickOnce installation. Everything works fine with it (after a lot of work) :), but now I\'m facing a problem:
Whenever
if (Process.GetProcesses().Count(p => p.ProcessName == "exe name") > 1)
{
foreach (var process in
Process.GetProcessesByName("exe name"))
{
process.Kill();
}
}
what i always using is
bool checkSingleInstance()
{
string procName = Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
Process[] processes = Process.GetProcessesByName(procName);
if (processes.Length > 1)
{
return true;
}
else
{
return false;
}
}
There is really good topic on that matter. You can find it here: using Mutext.
In WPF, you can use this code in your App.xaml.cs
file:
private static System.Threading.Mutex _mutex = null;
protected override void OnStartup(StartupEventArgs e)
{
string mutexId = ((System.Runtime.InteropServices.GuidAttribute)System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false).GetValue(0)).Value.ToString();
_mutex = new System.Threading.Mutex(true, mutexId, out bool createdNew);
if (!createdNew) Current.Shutdown();
else Exit += CloseMutexHandler;
base.OnStartup(e);
}
protected virtual void CloseMutexHandler(object sender, EventArgs e)
{
_mutex?.Close();
}