简体   繁体   English

将事件发送到单个进程

[英]Send event to single process

I am currently using an EventWaitHandle to trigger a clean shutdown of a native process that doesnt have a window from a Windows Forms app. 我当前正在使用EventWaitHandle触发本机进程的干净关闭,该本机进程没有Windows Forms应用程序中的窗口。 When I have multiple of these processes running at the same time, setting the event will stop all of them. 当我同时运行多个这些进程时,设置事件将停止所有这些进程。 Is there any way to send an event to a single selected process? 有什么方法可以将事件发送到单个选定的进程? I retain a Process variable for each of them. 我为它们每个保留一个Process变量。

EventWaitHandle Event = new EventWaitHandle(false, EventResetMode.ManualReset, "EventName");
Event.Set();

It sounds more like you want a named event handle per process. 听起来更像是您希望每个进程都有一个命名事件句柄。

You could use Process.Id to get a unique ID for each process, and use it to generate a unique string for each process. 您可以使用Process.Id获取每个进程的唯一ID,并使用它为每个进程生成唯一的字符串。

Because the process ID is global, it makes it safe to assume that the ID can be used by both processes correctly. 由于进程ID是全局的,因此可以安全地假定两个进程都可以正确使用该ID。

You can then append this number onto some string like "MyProcessEvent" to give, say, "MyProcessEvent6345615". 然后,您可以将此数字附加到诸如“ MyProcessEvent”之类的字符串上以给出“ MyProcessEvent6345615”。 Then use that string to open the named EventWaitHandle in both processes. 然后使用该字符串在两个进程中打开命名的EventWaitHandle

Then your controller process can signal any of the processes it started using that name. 然后,您的控制器进程可以使用该名称发信号通知它启动的任何进程。

( A process finds its own ID via Process.GetCurrentProcess() ). 一个进程通过Process.GetCurrentProcess()找到自己的ID )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM