gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:56:15 35 4
gpt4 key购买 nike

我在图像转换器上写作。当我对每个图像使用 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com