c#NetworkStream write() 和 read() [英] c# NetworkStream write() and read()

查看:43
本文介绍了c#NetworkStream write() 和 read()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何阻止 Networkstream.Read() 阻止线程.我有单独的线程,其中 NetworkStream.Read() 正在等待来自服务器的数据.假设用户按下某个按钮将一些数据发送到服务器.但是当其他线程中有 NetworkStream.Read() 等待数据时,我无法调用 NetworkStream.Write().我每次都可以锁定 NetworkStreamNetworkStream.Read() 会阻塞线程,所以我不能发送数据,直到读取至少 1 个字节.

I'm wondering how to stop Networkstream.Read() from blocking thread. I have separate thread where NetworkStream.Read() is waiting for data from server. Assume that user press some button that send some data to server. But I can't call NetworkStream.Write() while there is NetworkStream.Read() in other thread waiting for data. I can lock NetworkStream each time but NetworkStream.Read() will block thread so I can't send data until read at least 1 byte.

推荐答案

您可以同时发出 readwrite,如MSDN 文档

You can issue both read and write simultaneously as stated in the docs at MSDN docs

可以在 NetworkStream 类的实例上同时执行读写操作,无需同步.只要有一个唯一的写操作线程和一个唯一的读操作线程,就不会出现读写线程的交叉干扰,也不需要同步.

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

这篇关于c#NetworkStream write() 和 read()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆