【发布时间】:2010-05-13 15:18:35
【问题描述】:
我正在尝试通过命名管道将更新推送到进程中,但这样做我的进程循环现在会在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>
【问题讨论】:
标签: c# named-pipes