我有相同应用程序的多个实例正在运行.用户询问可以在每个实例上单击“退出”以将其关闭.
我想将选择添加到“退出所有实例”,这将引发一些“事件”的孩子,该事件通知应用程序应该关闭的所有实例.我不需要随此事件传输任何数据.
使用C#/ .NET在Windows中执行此操作的最佳(最好是最简单)方法是什么?
解决方法:
将good’ol WM_CLOSE发送给所有实例……
Process[] processes = Process.GetProcesses();
string thisProcess = Process.GetCurrentProcess().MainModule.FileName;
string thisProcessName = Process.GetCurrentProcess().ProcessName;
foreach (Process 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
Win32.SendMessage(process.MainWindowHandle, Win32.WM_CLOSE, 0, 0);
}