【发布时间】:2026-01-27 11:30:01
【问题描述】:
我确保我在并行循环中有并发收集。我期望在循环结束时有 35 个唯一的文件路径。我看到重复文件路径的结果不一致。我在这里错过了什么?
ConcurrentBag<string> generatedPdfFilePaths = new
ConcurrentBag<string>();
string generatedPdfPath = string.Empty;
Parallel.ForEach(docxPaths, (docxpath) =>
{
generatedPdfPath = docxpath.Replace(".docx", ".pdf");
if (ConvertDocxToPdf(docxpath, generatedPdfPath))
{
generatedPdfFilePaths.Add(generatedPdfPath);
}
});
// do stuff with generatedPdfFilePaths collection here
public bool ConvertDocxToPdf(string docxFilePath, string pdfFilePath)
{
try
{
Aspose.Words.Document docx = new
Aspose.Words.Document(docxFilePath);
docx.FontSettings = fontsetting;
docx.Save(pdfFilePath);
return true;
}
catch (Exception ex)
{
//do stuff
}
}
【问题讨论】:
-
将
string generatedPdfPath = string.Empty;移动到Parallel.ForEach循环体中。现在你无缘无故地从多个线程修改它。 -
结果有什么不一致?
-
@InBetween 道歉 - 我通过在线美化器运行代码,不喜欢结果,所以在 Linqpad 中重新格式化 - 当我将其粘贴到 Linqpad 时,我的剪贴板上一定有美化版本- 过失!
标签: c# parallel-processing task-parallel-library parallel.foreach