2010-05-13 4 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); 
      } 
     } 
    } 
} 

이 문제에 대한 해결책은 Queue<string>

답변

1

이것은 의도적으로 설계된 동작이므로 PipeStream.Read()는 차단 호출입니다. 대신 BeginRead()를 사용할 수 있습니다. 이것은 텍스트를 물론 데이터의 별 모양보다 작게 만듭니다. 실제 문제는 아니지만 PipeTransmissionMode.Message를 사용하십시오.

0
으로 파이프를 통해 수신 된 문자열을 파이프를 처리하고 밀어 데이터의 Queue<string>, 별도의 스레드를 모니터)합니다 (RefreshThread 있도록했다

클라이언트가 명명 된 파이프 연결을 처리합니까? 클라이언트가 절대로 닫히지 않으면 서버가 영원히 기다립니다.