TcpClient - 等待数据可用
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在基础
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 underlyingNetworkStream
. This will return aIAsyncResult
which has aAsyncWaitHandle
property. Now you create an array ofWaitHandle
's, stick theAsyncWaitHandle
in [0], theManualResetEvent
in [1] and then callWaitHandle.WaitAny()
with the array, and it will either return the index of the handle that is set, orWaitTimeout
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.