.NET中的异步BinaryReader和BinaryWriter? [英] Asynchronous BinaryReader and BinaryWriter in .Net?

查看:160
本文介绍了.NET中的异步BinaryReader和BinaryWriter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想异步读取和写入字节和结构化值类型,而不必担心解码器和字节移位:是否有可以让我做到这一点的东西?

I'd like to read and write bytes and structured value types, asynchronously, without having to worry about decoders and byte-shifting: is there something out there that would allow me to do that?

推荐答案

BinaryReader BinaryWriter 。您可以从基础 BaseStream 中同时读取,但文档指出以下内容:

It's not possible with BinaryReader or BinaryWriter. You could read concurrently from the underlying BaseStream, but the documentation states the following:


在读取或读取时使用基础流使用BinaryReader可能会导致数据丢失和损坏。例如,相同的字节可能读取不止一次,字节可能会跳过,或者字符读取可能变得不可预测。

Using the underlying stream while reading or while using the BinaryReader can cause data loss and corruption. For example, the same bytes might be read more than once, bytes might be skipped, or character reading might become unpredictable.

因此唯一的方法是推出自己的实现。但是这样做的好处值得商bat。 Microsoft的Marco Greg在博客文章应该我为异步方法公开了异步包装器吗?

Therefore the only way is to roll your own implementation. But the benefit of doing that is debatable. Marco Greg from Microsoft added the following comment to the blog article Should I expose asynchronous wrappers for synchronous methods?:


Jon:BinaryReader / Writer没有XxxAsync方法的原因是这些类型的方法通常只能从先前打开的基础流中读取/写入很少的字节。实际上,数据经常被缓存,并且从底层源获取数据所需的时间通常太短,以至于不值得异步进行处理。

Jon: The reason that the BinaryReader/Writer do not have XxxAsync methods is that the methods on those types typically read/write only very few bytes from an underlying stream that has been previously opened. In practice, the data is frequently cached and the time required to fetch the data from the underlying source is typically so small that it is not worth it doing it asynchronously.

,这些类型上有一些方法在某些情况下可能会传输大量数据(例如ReadString)。再往后,可能会或可能不会为这些方法添加异步版本,但是不太可能在不久的将来发生。

Notably, there are some methods on these types that in some circumstances may transfer larger amounts of data (e.g. ReadString). Further down the line, Async versions for those methods may or may not be added, but it is unlikely it will happen in the immediate future.

通常,您应该只如果您正在读取的数据量非常大(至少数百或数千个字节),或者您是第一次访问资源(例如,从文件中进行的第一次读取可能需要旋转存储),请考虑使用异步IO方法。磁盘,即使您正在读取一个字节也是如此。)

In general, you should only consider Async IO methods if the amount of data you are reading is significant (at least several hundreds or thousands of bytes), or if you are accessing a resource for the first time (e.g. a first read from a file may require to spin up the disk even if you are reading one byte).

这听起来很合理。如果确实需要解决方案,除了滚动自己的 BinaryReader / BinaryWriter 以外,还有几种解决方法。您可以在单独的线程中运行它(这可能效率不高),或者如果您愿意更改文件格式或有线协议,则可以使用以下模式(伪代码):

This sounds reasonable. If you do need a solution there are several workarounds apart from rolling your own BinaryReader/BinaryWriter. You can run it in a separate thread (which can be inefficient) or if you're willing to change the file format or wire protocol you could use this pattern (pseudo-code):

//read packet length
await stream.ReadAsync(buffer);
var packetLength=convertToInt(buffer);

//read complete packet asynchronously
await stream.ReadAsync(buffer,packetLength);

//process packet with BinaryReader
using(var br=new BinaryReader(new MemoryStream(buffer))
{
  //...
}

请注意,此模式仅在完整的缓冲区很容易装入内存时才有用受苦。

Please note that this pattern is only useful if the complete buffer easily fits in memory and that performance might suffer.

这篇关于.NET中的异步BinaryReader和BinaryWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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