我有以下多线程代码摘录,我一直在努力比较压缩副本和解压缩后的文件。该应用程序正在压缩一个包含可变数量的各种大小文件的文件夹,将文件复制到服务器并解压缩它们。然后对文件进行比较,并将此比较导出到ThreadPool
.
这是当前的完整方法:
public void FolderMoverLogic(string folderPathToZip, string unzipOutputDir)
{
string folderRootDir = Path.GetDirectoryName(folderPathToZip);
string folderNameToZip = Path.GetFileName(folderPathToZip);
try
{
//Zips files in <folderPathToZip> into folder <zippedLocal>
TransferMethods.CreateZipExternal(folderPathToZip, zippedlocal);
//Copies zipped folder to server location
File.Copy(zippedlocal + "\\" + folderNameToZip + ".zip", zippedserver + "\\" + folderNameToZip + ".zip");
//Unzips files to final server directory
TransferMethods.UnZip(zippedserver + "\\" + folderNameToZip + ".zip", unzipOutputDir + "\\" + folderNameToZip, sizeof(Int32));
TransferMethods m = new TransferMethods();
//Enumerate Files for MD5 Hash Comparison
var files = from file in Directory.EnumerateFiles(folderPathToZip, "*", SearchOption.AllDirectories)
select new
{
File = file,
};
int fileCount = 0;
CountdownEvent countdown = new CountdownEvent(10000);
using (ManualResetEvent resetEvent = new ManualResetEvent(false))
{
foreach (var f in files)
{
Interlocked.Increment(ref fileCount);
countdown.Reset(fileCount);
try
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(c =>
{
//Check if any of the hashes have been different and stop all threads for a reattempt
if (m.isFolderDifferent)
{
resetEvent.Set();
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel(); // cancels the CancellationTokenSource
try
{
countdown.Wait(cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("cde.Wait(preCanceledToken) threw OCE, as expected");
}
return;
}
else
{
//Sets m.isFolderDifferent to true if any files fail MD5 comparison
m.CompareFiles(f.File, folderRootDir, unzipOutputDir);
}
if (Interlocked.Decrement(ref fileCount) == 0)
{
resetEvent.Set();
}
countdown.Signal();
}));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
countdown.Wait();
resetEvent.WaitOne();
resetEvent.Close();
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}
}
到目前为止查看的有用资源:
发出信号并立即关闭 ManualResetEvent 是否安全?
线程池逻辑要求:
- 比较本地和服务器上的所有枚举文件
- 如果散列不匹配,则从所有线程返回
以前的线程池代码:
using (ManualResetEvent resetEvent = new ManualResetEvent(false))
{
foreach (var f in files)
{
testCount++;
try
{
//Thread t = new Thread(() => m.CompareFiles(f.File, unzipped, orglsource));
//t.Start();
//localThreads.Add(t);
ThreadPool.QueueUserWorkItem(
new WaitCallback(c =>
{
if (resetEvent.WaitOne(0)) //Here is the `ObjectDisposedException`
{
return;
}
if (!m.Folderdifferent)
{
m.CompareFiles(f.File, folderRootDir, unzipOutput);
}
else
{
resetEvent.Set();
}
if (Interlocked.Decrement(ref fileCountZipped) == 0)
{
resetEvent.Set();
}
}));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
resetEvent.WaitOne();
}
我ObjectDisposedExceptions
定期使用显示的先前代码。
我的问题是这样的:
- 当前方法是线程安全的吗?
- 逻辑合理吗?
- 任何关于性能或线程安全的改进想法
- 我在顶部的当前方法是否解决了以前的代码异常
我一直在测试这段代码,它一直在毫无例外地工作,但我正在寻找一些更有经验的反馈。