2010-05-13 85 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

客戶端是否處理其命名的管道連接?如果客戶端從不關閉,您的服務器將永遠等待。