2010-05-13 165 views
0

我正尝试通过命名管道将更新推送到进程中,但是现在我的进程循环接缝在while ((line = sr.ReadLine()) != null)上停止。我有点迷惑,因为这是我第一次进入命名管道。命名管道线程?

void RefreshThread() 
{ 
    using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("processPipe", PipeDirection.In)) 
    { 
     pipeStream.WaitForConnection(); 

     using (StreamReader sr = new StreamReader(pipeStream)) 
     { 
      for (; ;) 
      { 
       if (StopThread == true) 
       { 
        StopThread = false; 
        return;     // exit loop and terminate the thread 
       } 

       // push update for heartbeat 
       int HeartbeatHandle = ItemDictionary["Info.Heartbeat"]; 
       int HeartbeatValue = (int)Config.Items[HeartbeatHandle].Value; 
       Config.Items[HeartbeatHandle].Value = ++HeartbeatValue; 
       SetItemValue(HeartbeatHandle, HeartbeatValue, (short)0xC0, DateTime.Now); 

       string line = null; 
       while ((line = sr.ReadLine()) != null) 
       { 
        // line is in the format: item, value, timestamp 
        string[] parts = line.Split(','); 

        // push update and store value in item cache 
        int handle = ItemDictionary[parts[0]]; 
        object value = parts[1]; 
        Config.Items[handle].Value = int.Parse(value); 
        DateTime timestamp = DateTime.FromBinary(long.Parse(parts[2])); 
        SetItemValue(handle, value, (short)0xC0, timestamp); 
       } 

       Thread.Sleep(500); 
      } 
     } 
    } 
} 

的解决这个问题是使RefreshThread()监视Queue<string>用于数据,和一个单独的线程来处理管和推动通过管接收到的串入Queue<string>

回答

1

这是通过设计,PipeStream.Read()是一个阻塞调用。你可以使用BeginRead()来代替。当然,这使得文本比数据的恒星格式要小。不是一个真正的问题,请使用PipeTransmissionMode.Message。

0

客户端是否处理其命名的管道连接?如果客户端从不关闭,您的服务器将永远等待。