c# - Parallel for each 循环不保存所有文件

标签 c# parallel-processing parallel-foreach

我在图像转换器上写作。当我对每个图像使用 parallel 时,并非所有图像都被保存。在磁盘上写入文件的处理速度是否太快?

这是我的代码:

private void convert()
{
    Parallel.ForEach(source.GetFiles("*.tif"), 
         new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, 
         file =>
         {                  
            fileName = file.Name;
            MagickImage image = new MagickImage(sourceFolderPath + "\\" + file);
            image.ColorSpace = ColorSpace.XYZ;
            image.GammaCorrect(2.4);
            image.Write(destinationFolderPath + "\\" + fileName);
         });
}

我做错了什么?

最佳答案

尝试处理图像:

private void convert()
{
    Parallel.ForEach(source.GetFiles("*.tif"), 
         new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, 
         file =>
         {                  
            fileName = file.Name;
            using (MagickImage image = new MagickImage(sourceFolderPath + "\\" + file))
            {
                image.ColorSpace = ColorSpace.XYZ;
                image.GammaCorrect(2.4);
                image.Write(destinationFolderPath + "\\" + fileName);
            }
         });
}

关于c# - Parallel for each 循环不保存所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47178069/

相关文章:

c# - 在 C# 中将对象序列化为 xml

c# - 另一个未更新的 MVVM 可见性属性

支持所有 C++11 并发功能的 C++ 编译器?

R foreach : from single-machine to cluster

r - 是否可以使用 foreach 和后端 "multicore-kind"获得进度条

r - 使用 foreach 时无法从同一包中找到函数(Windows 开发)

c# - 适用于 .Net 应用程序的基于 OMR 的软件

c# - 当我的 Windows 应用商店应用客户端离线时如何使用缓存的事件目录

java - Java 是否支持多核处理器/并行处理?

CUDA:分散通信模式