C# 只允许执行我的应用程序的最新实例?
我目前在我的应用程序中有一个互斥体,只允许运行一个实例。我的问题是,我现在如何处理这段代码,并将其转换为关闭当前正在运行的实例并允许打开一个新实例 我试图解决的问题是:我的应用程序接受args,需要经常使用新参数重新打开。目前,如果没有互斥锁,它可以无限次地打开。我只希望运行一个具有最新参数集的实例 谢谢, 凯文 一些代码C# 只允许执行我的应用程序的最新实例?,c#,.net,mutex,C#,.net,Mutex,我目前在我的应用程序中有一个互斥体,只允许运行一个实例。我的问题是,我现在如何处理这段代码,并将其转换为关闭当前正在运行的实例并允许打开一个新实例 我试图解决的问题是:我的应用程序接受args,需要经常使用新参数重新打开。目前,如果没有互斥锁,它可以无限次地打开。我只希望运行一个具有最新参数集的实例 谢谢, 凯文 一些代码 互斥锁不用于进程间事件通知,因此无法使用互斥锁关闭另一个进程。我的建议是做一些类似于中推荐的事情 我将把这两个答案组合成我使用过的东西: Process[] processe
互斥锁不用于进程间事件通知,因此无法使用互斥锁关闭另一个进程。我的建议是做一些类似于中推荐的事情 我将把这两个答案组合成我使用过的东西:
Process[] processes = Process.GetProcesses();
string thisProcess = Process.GetCurrentProcess().MainModule.FileName;
string thisProcessName = Process.GetCurrentProcess().ProcessName;
foreach (var process in processes)
{
// Compare process name, this will weed out most processes
if (thisProcessName.CompareTo(process.ProcessName) != 0) continue;
// Check the file name of the processes main module
if (thisProcess.CompareTo(process.MainModule.FileName) != 0) continue;
if (Process.GetCurrentProcess().Id == process.Id)
{
// We don't want to commit suicide
continue;
}
// Tell the other instance to die
process.CloseMainWindow();
}
Process[] processes = Process.GetProcesses();
string thisProcess = Process.GetCurrentProcess().MainModule.FileName;
string thisProcessName = Process.GetCurrentProcess().ProcessName;
foreach (var process in processes)
{
// Compare process name, this will weed out most processes
if (thisProcessName.CompareTo(process.ProcessName) != 0) continue;
// Check the file name of the processes main module
if (thisProcess.CompareTo(process.MainModule.FileName) != 0) continue;
if (Process.GetCurrentProcess().Id == process.Id)
{
// We don't want to commit suicide
continue;
}
// Tell the other instance to die
process.CloseMainWindow();
}