TcpClient - 等待数据可用

发布于 2024-08-23 06:34:41 字数 604 浏览 2 评论 0原文

在我的 C# 应用程序中,我有一个线程,它基本上不断地从 TcpClient 读取数据,直到被告知停止。我将 WaitHandles 用于此目的,例如:

private ManualResetEvent stopping;

private void Receive()
{
    while (!this.stopping.WaitOne(10))
    {
        while (this.client.Available > 0)
        {
            // Read and process data
        }
    }
}

如您所见,我正在等待线程被告知停止。如果还没有,它会从 TcpClient 读取所有数据并循环。

我遇到的问题是 10 毫秒的延迟,我不想有这个延迟。我可以减少它,但我更喜欢一种解决方案,其中程序将暂停,直到线程被告知停止或有更多数据可用。

实际上,我想要的是一个 WaitHandle,它告诉我 TcpClient 上的数据何时可用。这样,我就可以使用 WaitHandle.WaitAny。我有什么办法可以做到这一点,或者有人可以建议另一种方法吗?

这不能成为一个障碍,因为它需要是一个相当高性能且轻量级的后台进程。

In my C# application, I have a thread which basically continually reads from a TcpClient until told to stop. I use WaitHandles for this purpose, for example:

private ManualResetEvent stopping;

private void Receive()
{
    while (!this.stopping.WaitOne(10))
    {
        while (this.client.Available > 0)
        {
            // Read and process data
        }
    }
}

As you can see, I'm waiting for the thread to be told to stop. If it hasn't been, it reads all data from the TcpClient and loops.

The problem I have is the 10ms delay, which I'd rather not have. I could reduce it, but I'd prefer a solution where the program will pause until EITHER the thread is told to stop, or more data becomes available.

In effect, what I want is a WaitHandle which tells me when data is available on the TcpClient. That way, I can use WaitHandle.WaitAny. Is there any way I can do this, or can someone suggest an alternative approach?

This can't be a bodge as it needs to be a fairly performant -and- lightweight background process.

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

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

发布评论

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

评论(1

淡淡の花香 2024-08-30 06:34:41

您需要在基础 NetworkStream 上使用 BeginRead 方法。这将返回一个具有 AsyncWaitHandle 属性的 IAsyncResult。现在,您创建一个 WaitHandle 数组,将 AsyncWaitHandle 粘贴到 [0] 中,将 ManualResetEvent 粘贴到 [1] 中,然后调用 WaitHandle.WaitAny() 与数组,它将返回所设置的句柄的索引,或者超时时的WaitTimeout

一旦您知道已设置 AsyncHandle,您就可以使用 EndRead 来完成读取,并将数据放入缓冲区。

BeginRead 的帮助说明您需要一个回调函数,但如果您愿意,您可以为此传递 Null 并在基函数中编写所有内容。

You'll need to use the BeginRead method on the underlying NetworkStream. This will return a IAsyncResult which has a AsyncWaitHandle property. Now you create an array of WaitHandle's, stick the AsyncWaitHandle in [0], the ManualResetEvent in [1] and then call WaitHandle.WaitAny() with the array, and it will either return the index of the handle that is set, or WaitTimeout on time out.

Once you know it's the AsyncHandle that's set, you can use EndRead to complete the read, and get the data into your buffer.

The help for BeginRead says you need a callback function, but you can pass Null for this and code everything in your base function if you prefer.

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