Caharp 多核加速

public class BatchImageExtractor
{
    public static async Task ExtractImagesParallel(
        List<string> pdfPaths, 
        string outputFolder)
    {
        // 使用并行处理(注意控制并发度)
        var options = new ParallelOptions 
        { 
            MaxDegreeOfParallelism = Environment.ProcessorCount - 1 
        };
        
        await Parallel.ForEachAsync(pdfPaths, options, async (pdfPath, ct) =>
        {
            try
            {
                var fileName = Path.GetFileNameWithoutExtension(pdfPath);
                var pdfOutputFolder = Path.Combine(outputFolder, fileName);
                
                await Task.Run(() => 
                    ImageExtractor.ExtractImages(pdfPath, pdfOutputFolder), ct);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"处理失败 {pdfPath}: {ex. Message}");
            }
        });
    }
}

转自:

PdfPig应用详解:告别PDF处理的那些坑

posted @ 2026-07-01 14:36  尼库拉西  阅读(3)  评论(0)    收藏  举报