gpt4 book ai didi

c# - 带有响应式扩展的命名管道

转载 作者:太空狗 更新时间:2023-10-29 21:45:09 25 4
gpt4 key购买 nike

我已经使用 System.IO.Pipes(控制台应用程序和 GUI 应用程序)在同一台计算机上的两个独立进程之间建立了通信。控制台应用程序 NamedPipeServerStream 创建管道,而 GUI 应用程序 NamedPipeClientStream 连接到现有管道。我非常频繁地更新 GUI。我的问题是,命名管道技术是处理这种情况的最有效方法吗?第二个问题是 Reactive Extensions RX 是否更适合这种情况以及如何?提前谢谢你。

服务器

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace PipeApplicationSender
{

class ProgramPipeTest
{
static void Main(string[] args)
{

ProgramPipeTest Server = new ProgramPipeTest();

Thread ServerThread = new Thread( Server.ThreadStartServer );

ServerThread.Start();
}


public void ThreadStartServer()
{
// Create a name pipe
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
{

// Wait for a connection
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");

using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
// We read a line from the pipe and print it together with the current time
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
}

客户端

using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace PipeApplicationReceiver
{

class ProgramPipeReceive
{
static void Main(string[] args)
{

ProgramPipeReceive Server = new ProgramPipeReceive ();

Thread ServerThread = new Thread( Server.ThreadStartServer );

ServerThread.Start();
}


public void ThreadStartClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;

// Only continue after the server was created -- otherwise we just fail badly
// SyncClientServer.WaitOne();

using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("mytestpipe"))
{
// The connect function will indefinately wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipeStream.Connect();

Console.WriteLine("[Client] Pipe connection established");
using (StreamWriter sw = new StreamWriter(pipeStream))
{
sw.AutoFlush = true;
string temp;
while ((temp = Console.ReadLine()) != null)
{
if (temp == "quit") break;
sw.WriteLine(temp);
}
}
}
}


}
}

最佳答案

https://gist.github.com/hanishi/7139122

我刚刚用Rx做了一个完整的IpcServer和IpcClient。我希望你喜欢它。

关于c# - 带有响应式扩展的命名管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17675870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com