[英]Communication between two different applications
我们在一台机器上运行了两个应用程序,其中一个是通过读取 xml 文档来响应每个请求的 web 应用程序。我们希望添加这样一种情况,即当创建新的 xml 文件或替换现有文件时,应用程序不能读取文件,直到其全部更改为止,并且在发生这种情况时,它必须以旧文件进行响应。
由于 Web 应用程序适用于请求/响应周期,我们决定不应干扰这个周期,因为在实时运行的系统中文件更改和请求时间之间的时间是模糊的,我们必须拆分文件读取过程。为此,我们使用FileSystemWatcher 在带有 Windows 或控制台应用程序的本地机器上(或其他一些人说改用 WCF)。
现在我们在上述案例中提出问题,说我们如何通信这两个(或更多)应用程序?
看起来您对启用 IPC 的命名管道感兴趣,请查看此链接以获取示例,或查看此 MSDN 链接。
从MSDN的NamedPipeServerStream 页面抓取代码说明最简单(请参阅客户端的NamedPipeClientStream 页面):
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.Out))
{
Console.WriteLine("NamedPipeServerStream object created.");
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(pipeServer))
{
sw.AutoFlush = true;
Console.Write("Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.