0

I have this program that several users on the same computer is using (windows 2008 server environment with remote desktop clients).

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? Anyone have any good ideas?

Edit: Forgot to mention, it's a WinForm (not a command line) program.

Edit2: My comment was too long, so I guess it's best to just edit the question instead...

The file it's deleting is actually a file containing it's 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. If it discovers the file it reads the pid and tries to see if the pid exists. 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. (Client initiated, remote server initiated, or, as in this case "Local server initiated").

4

2 回答 2

2

AMutex是代表互斥的东西。在这种情况下,我认为这不是您真正想要的。它并没有真正建模你想要的东西。再加上一次只能关闭一个应用程序。我建议使用命名EventWaitHandle来模拟一个应用程序向其他应用程序发送事件(关闭事件)。如果您使用手动重置事件(通过EventResetMode.ManualReset在创建EventWaitHandle对象时使用。

您将在后台线程上执行此操作,当发出信号时,会将某些内容编组到 UI 线程(通过Control.BeginInvoke)以与需要关闭的主窗体进行通信。

于 2012-07-30T19:59:38.947 回答
0

您可以使用Mutex来使用系统范围的事件

请参阅 MSDN 中链接页面上的示例

于 2012-07-30T19:36:58.903 回答