我正尝试通过命名管道将更新推送到进程中,但是现在我的进程循环接缝在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>