2

It's my first post on StackOverflow, so please be patient with me :)

I'm looking out for a way to pass a value to an already running application. The application should be able to accept this value and use it for further operations during its lifetime. I'd prefer not to logoff/restart the application in order to use the updated value.

The scenario would be something like this:

The application uses MachineName to initialize and populate some objects while it is being instantiated. Once it is up and running, I would like to pass a different workstation name to the application (possibly from command line) and the application should be able to use this MachineName and use it further down the line. I dont mind if I have perform same operations again to initialize the required objects with new MachineName name.

The application in question is a WPF app with C#. It's also using some WCF services as well. I was thinking to use a common registry key to store the workstation name and this registry key needs to be used/updated in the application. So it should be the first point of contact. But I'm not sure if this is a better approach. Any ideas please?

4

3 回答 3

1

使用FileSystemWatcher. 当您的应用收到更改通知时,重新读取文件的内容并更新您的应用状态。

于 2012-11-01T13:03:46.133 回答
0

首先,我认为你可能有点过于复杂了。如果你需要做的只是给你的应用程序一个新的字符串“MachineName”,那么你有几个选项(这当然不是你的所有选项,只是几个要考虑的):

  1. 由于这是一个 WPF 应用程序,我们可以假设某个用户正在与它进行交互……否则它也可能只是一个控制台应用程序。如果是这种情况,请创建一个视图,让用户能够更改 MachineName。可能是一个 ComboBox 列出了所有可用的 MachineNames 和一个按钮来开始您需要的重新初始化。
  2. 您显然已经为服务呼叫做好了准备。您可以让您的应用程序既充当客户端又充当服务,从而允许您远程(阅读:在此应用程序之外)更新您的 MachineName。
  3. 您可以将此应用程序设置为监视特定目录或文件(如@jlew 所指出的那样),当该文件发生某些更改时,您的应用程序将读取更改(新的 MachineName)并开始它的重新初始化例程。

我不知道你的全部要求,但我可能会为 1 和 2 设置它,这会给你更多的灵活性。

于 2012-11-01T13:48:14.790 回答
0

应用程序在启动时监听一个套接字端口或监视一个共享文件,如果应用程序再次带参数调用,只需打开套接字端口或将命令写入文件,第一个应用程序就可以重新初始化。

于 2012-11-01T13:05:17.227 回答