.NET进程间"事件" [英] .NET inter-process "events"

查看:159
本文介绍了.NET进程间"事件"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有运行同一应用程序的多个实例。用户要求可以点击退出上的每个实例将其关闭。

I have multiple instances of the same application running. The user ask can click "Quit" on each instance to shut it down.

我想补充的选择退出所有实例,这将提高事件,通知他们应该关闭应用程序的所有实例的一些孩子。我并不需要运输的任何数据以及与此事件

I would like to add the choice to "Quit All Instances", which will raise some kid of "event" that notifies all instances of the application that they should close. I don't need to transport any data along with this event.

什么是最好的(和preferably简单)的方式来使用C#/。NET在Windows做到这一点?

What's the best (and preferably simplest) way to do this in Windows using C#/.NET?

推荐答案

发送good'ol WM_CLOSE到所有实例...

Send the good'ol WM_CLOSE to all instances...

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);
}

这篇关于.NET进程间"事件"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆