System.Threading.Tasks 和 FileStream
我正在尝试使用 FileStream 对象从网络上的不同文件读取数据。 因此,我创建了任务(System.Threading.Tasks)来读取所需的部分文件。
对这种行为感到非常惊讶。这些任务会引发错误:
int_ReadBytes = stm_BaseStream.Read (byt_buffer, 0, ( int ) ( int_RecordLength * uint_BufferThis ));
if (int_ReadBytes != ( int_RecordLength * uint_BufferThis ))
{
throw new Exception ("Could not read record");
// throws an error here.
}
有人将任务与 FileStream 对象一起使用,或者知道为什么我会遇到此问题? 谢谢。
I am trying to read data from different files on network using FileStream object.
So I created Tasks (System.Threading.Tasks) to read parts of the files required.
Quite surprised by the behaviour. These Tasks throws an error :
int_ReadBytes = stm_BaseStream.Read (byt_buffer, 0, ( int ) ( int_RecordLength * uint_BufferThis ));
if (int_ReadBytes != ( int_RecordLength * uint_BufferThis ))
{
throw new Exception ("Could not read record");
// throws an error here.
}
Anyone used Task with FileStream objects, or has any idea why I am getting this issue?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
int_ReadBytes
的返回值是多少?它可能不等于(int_RecordLength * uint_BufferThis)
只是因为文件中没有足够的字节可供读取,这将导致您的代码抛出异常。what is the returned value of
int_ReadBytes
? It may not be equal to(int_RecordLength * uint_BufferThis)
simply because there are not enough bytes in the file to read, which will casue your code to throw an exception.