命名管道停止线程?

发布于 2024-09-01 10:07:41 字数 1869 浏览 3 评论 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 中的数据,并使用一个单独的线程来处理管道并将通过管道接收到的字符串推送到 Queue;字符串>

I am attempting to push updates into a process via a named pipe, but in doing so my process loop now seams to stall on while ((line = sr.ReadLine()) != null). I'm a little mystified as to what might be wrong as this is my first foray into named pipes.

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);
            }
        }
    }
}

The solution to this problem was to make the RefreshThread() monitor a Queue<string> for data, and a separate thread to handle the pipe and push the strings received through the pipe into the Queue<string>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我三岁 2024-09-08 10:07:41

这是设计使然,PipeStream.Read() 是一个阻塞调用。您可以使用 BeginRead() 代替。当然,这使得文本不那么适合数据的标准格式。不是真正的问题,请使用 PipeTransmissionMode.Message。

This is by design, PipeStream.Read() is a blocking call. You could use BeginRead() instead. This makes text less than a stellar format for data of course. Not a real problem, use PipeTransmissionMode.Message.

昔梦 2024-09-08 10:07:41

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

Does the client dispose of its named pipe connection? If the client never closes, your server will wait forever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文