简体   繁体   English

将命令发送到进程

[英]Send command to a process

I have this program that several users on the same computer is using (windows 2008 server environment with remote desktop clients). 我有这个程序,在同一台计算机上的几个用户正在使用(带有远程桌面客户端的Windows 2008服务器环境)。

When the program is ended normally, it deletes a special file in its working directory. 程序正常结束时,会删除其工作目录中的特殊文件。 I need to be able to send all instances of this program a command to cleanly shut down (so that it deletes the file at exit). 我需要能够向该程序的所有实例发送一个命令以彻底关闭(以便它在退出时删除该文件)。

What would be the best way to do this, and how? 什么是最好的方法,以及如何做到这一点? I assume getting the pids for each instance is a start, but then what? 我假设为每个实例获取pids是一个开始,但那又是什么? Anyone have any good ideas? 有人有什么好主意吗?

Edit: Forgot to mention, it's a WinForm (not a command line) program. 编辑:忘了提,它是一个WinForm(不是命令行)程序。

Edit2: My comment was too long, so I guess it's best to just edit the question instead... Edit2:我的评论太长了,所以我想最好只是编辑问题而不是......

The file it's deleting is actually a file containing it's pid. 它正在删除的文件实际上是一个包含它的pid的文件。 The reason for having this file is to make sure that the user doesn't attempt to start the program twice with the same arguments (login information). 拥有此文件的原因是为了确保用户不会尝试使用相同的参数(登录信息)两次启动程序。

The main program (control center) that is actually starting the client program is keeping track of the pidfile in the users directory. 实际启动客户端程序的主程序(控制中心)正在跟踪users目录中的pidfile。 If it discovers the file it reads the pid and tries to see if the pid exists. 如果它发现文件,它会读取pid并尝试查看pid是否存在。 If not, it actually does delete the file and is letting the user start the client window. 如果没有,它实际上会删除该文件并让用户启动客户端窗口。

I guess by using that procedure, I simply could make it look for all pids asociated with my application and simply kill them, ut I'd prefere to be able to send a shutdown command, as that would also notify the user in an IM that the program is shutting down for whatever the reason given. 我想通过使用该程序,我只是可以让它查找与我的应用程序相关的所有pid并简单地杀死它们,我会优先发送一个关闭命令,因为这也会在IM中通知用户无论出于什么原因,程序都会关闭。 (Client initiated, remote server initiated, or, as in this case "Local server initiated"). (客户端启动,远程服务器启动,或者,如本例中“本地服务器启动”)。

A Mutex is something that represents mutual exclusion. Mutex是代表Mutex的东西。 I don't think that is really what you want in this case. 在这种情况下,我认为这不是你想要的。 It's not really modelling what you want; 它并不是真正建模你想要的东西; plus only one application at time would be able to shut down. 加上只有一个应用程序将能够关闭。 I would recommend using a named EventWaitHandle to model one application sending events (shutdown events) to other applications. 我建议使用一个名为EventWaitHandle来模拟一个应用程序向其他应用程序发送事件(关闭事件)。 If you use a manual reset event (by using EventResetMode.ManualReset when you create an EventWaitHandle object. 如果使用手动重置事件(通过使用EventResetMode.ManualReset当您创建EventWaitHandle对象。

You would do this on a background thread that, when signalled, would marshal something over to the UI thread (via Control.BeginInvoke ) to communicate to the main form that it needs to shut down. 您可以在后台线程上执行此操作,该线程在发出信号时会将某些内容封送到UI线程(通过Control.BeginInvoke ),以便与需要关闭的主窗体进行通信。

You could use a system wide event by using a Mutex 您可以使用Mutex来使用系统范围的事件

See the example on the linked page in the MSDN 请参阅MSDN中链接页面上的示例

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

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