远程收盘后,保持管道开启的最佳方法 [英] Best way to keep a pipe open after a remote close
问题描述
使用本教程我想出了$低于C $ C。我的客户经常遇到。其通过点击激活,并且可能可以在同一时刻在特定情况下被推出两次。我而另一个客户端打开,导致管道在纤薄几毫秒内被关闭担心一个客户端可能会关闭。请告诉我要保持管道打开?最好的办法
的静态公共无效ThreadStartServer()
{
而(真)
{
使用(NamedPipeServerStream pipeStream =新NamedPipeServerStream(mytestpipe))
{
Console.WriteLine([服务器]管道创建{0},pipeStream.GetHash code());
pipeStream.WaitForConnection();
Console.WriteLine([服务器]管道连接建立);
使用(StreamReader的SR =新的StreamReader(pipeStream))
{
串温度;
而((TEMP = sr.ReadLine())!= NULL)
{
Console.WriteLine({0}:{1},DateTime.Now,温度);
}
}
}
}
请您管道服务器多线程,有一个线程专用于倾听。请参阅<一href="http://stackoverflow.com/questions/5584211/named-pipes-usage-multiple-clients-one-server-multiple-parallel-requests/5584764#5584764">these <一href="http://stackoverflow.com/questions/4451805/windows-named-pipe-issue-error-$c$c-233-alternates/4475816#4475816">answers:
Using this tutorial i came up with the code below. My client is ran frequently. Its activated via clicks and possibly can be launched twice at the same moment in certain circumstance. I am worried one client may close while another client opens which causes the pipe to be closed in that slim few milliseconds. Whats the best way to keep the pipe open?
static public void ThreadStartServer()
{
while (true)
{
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("mytestpipe"))
{
Console.WriteLine("[Server] Pipe created {0}", pipeStream.GetHashCode());
pipeStream.WaitForConnection();
Console.WriteLine("[Server] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("{0}: {1}", DateTime.Now, temp);
}
}
}
}
Make your pipe server multi-threaded, with one thread dedicated to listening. See these answers:
这篇关于远程收盘后,保持管道开启的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!