发送信息从一个运行控制台应用程序到另一个控制台、应用程序、信息

2023-09-02 23:57:20 作者:情深已故

我有一个控制台应用程序做一些冗长同步到FTP服务器。 另一个控制台应用程序prepares本地文件系统中的一些需要更新的文件。 然后,第二个将等待第一个换一个最终的目录名,使其成为在网络上可见之前完成。

I have one console app that is doing some lengthy syncing to an ftp server. Another console app prepares a local filesystem with some needed updated files. Then the second one will wait for the first one to finish before swapping a final directory name so it becomes visible on the web.

我搜索,使同步应用程序进行通信,它的完成了它的任务,第二应用程序的最佳途径。它看起来像Using数据复制的IPC 是这样做的最适合的解决方案。

I searched for the best way to make the syncing app to communicate to the second app that it's finished it's job. It looks like Using Data Copy for IPC is the best suited solution for this.

问题是双重的:

在我说得对不对?有没有更简单的方式来获得同样的结果? 有一个管理的(.NET)的方式来做到这一点?

推荐答案

如果你需要的是通知对方已完成其任务的一个应用程序,最简单的方法是使用一个命名的EventWaitHandle。该对象在其unsignaled状态下创建。第一个应用程序等待手柄上,第二个应用程序信号时,它的完成做的工作手柄。例如:

If all you need is to notify one application that the other has completed its task, the easiest way would be to use a named EventWaitHandle. The object is created in its unsignaled state. The first app waits on the handle, and the second app signals the handle when it's finished doing its job. For example:

// First application
EventWaitHandle waitForSignal = new EventWaitHandle(false, EventResetMode.ManualReset, "MyWaitHandle");

// Here, the first application does whatever initialization it can.
// Then it waits for the handle to be signaled:
// The program will block until somebody signals the handle.
waitForSignal.WaitOne();

这是设置等待同步的第一个程序。第二个应用程序也同样简单:

That sets up the first program to wait for synchronization. The second application is equally simple:

// Second app
EventWaitHandle doneWithInit = new EventWaitHandle(false, EventResetMode.ManualReset, "MyWaitHandle");

// Here, the second application initializes what it needs to.
// When it's done, it signals the wait handle:
doneWithInit.Set();

当第二个应用程序调用集合,它标志着事件的第一个应用程序将继续进行。

When the second application calls Set, it signals the event and the first application will continue.