CSkin博客
标题:
parallel.for并行计算文件MD5卡死,求支援
[打印本页]
作者:
zhy934453950
时间:
2018-5-11 15:51
标题:
parallel.for并行计算文件MD5卡死,求支援
本帖最后由 zhy934453950 于 2018-5-11 15:52 编辑
问题源码://我怀疑Parallel.For(0, inputStream.Length, ever => {此处进入了死循环 FileStream inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
MD5 md5 = new MD5CryptoServiceProvider();
int bufferChunkSize = 1024 * 1024;//自定义缓冲区大小1MB即1024K
byte[] inputBuffer = new byte[bufferChunkSize];
int readLength = 0;//每次读取长度
byte[] outputBuffer = new byte[bufferChunkSize];
Parallel.For(0, inputStream.Length, ever => {
md5.TransformBlock(inputBuffer, 0, readLength, outputBuffer, 0);//计算MD5
});
md5.TransformFinalBlock(inputBuffer, 0, 0);//完成最后计算,必须调用(由于上一部循环已经完成所有运算,所以调用此方法时后面的两个参数都为0)
byte[] bytes = md5.Hash;
string md5Value = string.Empty;
if (bytes != null)
{
StringBuilder str = new StringBuilder();
Parallel.For(0, bytes.Length,i=> {
str.Append(bytes
.ToString("X2"));
});
md5Value = str.ToString();
}
md5.Clear();
inputStream.Close();
return md5Value;
欢迎光临 CSkin博客 (http://bbs.cskin.net/)
Powered by Discuz! X3.2