我正在做一个项目,要求我对md5校验和执行一些多任务处理。我通过创建一个新线程并使用一种允许我重用不同算法的方法,创建了一种非常简单的方式来处理md5校验和。
这是我的新线程的代码:
private readonly Thread md5Check_ = new Thread(new ThreadStart(md5Check));
这是该线程的处理程序:
private static void md5Check()
{
string config_integrity = GetChecksum(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/file.txt", Algorithms.MD5,).ToLower();
}
(这是在同一MainWindow.xaml.cs文件中编写的)这是GetChecksum方法:
public static string GetChecksum(string fileName, HashAlgorithm algorithm)
{
if (File.Exists(fileName))
{
using (var stream = new BufferedStream(File.OpenRead(fileName), 100000))
{
return BitConverter.ToString(algorithm.ComputeHash(stream)).Replace("-", string.Empty);
}
}
else
{
return "error";
}
}
和算法:
public static class Algorithms
{
public static readonly HashAlgorithm MD5 = new MD5CryptoServiceProvider();
public static readonly HashAlgorithm SHA1 = new SHA1Managed();
public static readonly HashAlgorithm SHA256 = new SHA256Managed();
public static readonly HashAlgorithm SHA384 = new SHA384Managed();
public static readonly HashAlgorithm SHA512 = new SHA512Managed();
public static readonly HashAlgorithm RIPEMD160 = new RIPEMD160Managed();
}
我想知道既然新线程(md5Check_)在主线程上调用了getChecksum方法,那么实际的计算是在新线程(md5check)还是在主线程上进行,就像文件是1GB或2GB一样,我的应用程序似乎崩溃。
除非您通过其他线程上的Dispatcher(在WPF中)手动分派该方法,否则将从该线程调用的任何方法都将在调用线程上执行。
同样,根据您的情况,您可以通过使用GetChecksum()方法中的Thread.CurrentThread属性来验证自己,以查看其当前正在哪个线程上执行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句