IRONSOFTWAREHOME

如何设置最大并行线程数

Curtis Chau
Curtis Chau
Updated: 2025年11月30日

读取大量条形码时,依赖单线程进程可能会造成性能瓶颈并限制可扩展性。 但是,使用并行线程可以让应用程序同时处理多个图像,从而有效地提高总处理能力,并大幅缩短完成批处理作业所需的时间。

为这些线程设置最大限制是优化性能的有效方法。 它通过在处理器核心之间平衡工作负载,确保应用程序充分利用硬件的潜力。 这种方法可以最大限度地提高效率,在保持应用程序流畅运行的同时,提供最快的结果。

IronBarcode 提供了一种简单的方法来控制此限制,从而确保实现最佳的机器性能。 以下部分演示如何轻松设置这些线程限制。



设置最大并行线程数

在这个例子中,我们将使用大量的条形码图像来说明使用多线程进程而不是单线程进程的可扩展性和效率。 您可以点击此处下载图片文件夹。

要配置IronBarcode使用多个线程,首先要实例化一个新的Multithreaded设置为true。 之后,通过分配整数值来设置MaxParallelThreads属性。 默认情况下,MaxParallelThreads设置为4。

配置完成后,从文件夹中导入大量条形码图像。 然后,使用循环,使用BarcodeReaderOptions。 最后,通过访问BarcodeResults来显示条码值和类型。

using Google.Protobuf.WellKnownTypes;
using IronBarCode;
using System;
using System.IO;

int maxParallelThreads = 4;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to 4
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
};

// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Retrieve all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    foreach (var result in results)
    {
        // Show the type and value for every barcode found
        Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");

    }

}
C#

输出

多线程输出

如控制台输出所示,它会显示每个对应图像的条形码值和类型。

设置合适的最大并行线程数

MaxParallelThreads的整数没有硬性限制,但设置的值高于硬件的逻辑核心容量实际上可能导致性能下降。 这是因为处理器无法处理过多的上下文切换,这可能会导致性能开销而不是速度提升。 因此,MaxParallelThreads的正确值取决于计算机的规格,开发者应测试以找到最适合其环境的值。

在此示例中,我们将展示上述相同的多线程场景,但计时器用于对比默认值4与使用Environment.ProcessorCount来利用所有可用线程。 Environment.ProcessorCount将被设置为32。

using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

// Set the max parallel threads to the number of processor cores
int maxParallelThreads = Environment.ProcessorCount;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to the number of processor cores
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
    ExpectMultipleBarcodes = true,
};

// Start timing the process
var stopwatch = Stopwatch.StartNew();
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Check if directory exists to prevent crashes
if (!Directory.Exists(folderPath))
{
    Console.WriteLine($"Error: The directory '{folderPath}' does not exist.");
    return;
}

// Get all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");

        }
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s");
C#

输出

4线程处理时间

4 处理器

该过程的处理时间为 84 秒。

处理时间与环境处理器数量

32 处理器

如您所见,此操作的处理时间为 53 秒,这比仅使用四个线程运行它要快得多。 但是请注意,使用更多线程并不能保证性能提升,因为性能取决于主机处理器。 一般经验法则是使用可用处理器的最大数量减一,以确保仍然有一个线程可用于其他系统操作。

警告: 项目环境必须配置为允许多线程。 否则,设置MaxParallelThreads不会提升处理速度,并可能实际上降低速度。)

常见问题解答

在 IronBarcode 中设置最大并行线程的目的是什么?

设置最大并行线程允许您通过有效利用系统资源来优化条形码生成的性能,尤其是在批量处理大量条形码时。

如何配置 IronBarcode 的最大并行线程数?

您可以通过在 C# 代码中使用适当的方法来配置 IronBarcode 的最大并行线程数,为条码生成任务设置所需的线程数。

为什么要优化批量创建 BarCode 的性能?

优化批量条形码创建的性能可确保流程高效快速,减少生成大量条形码所需的时间和资源,这对于具有高吞吐量需求的应用程序至关重要。

在 IronBarcode 中使用并行处理有什么好处?

IronBarcode 中的并行处理可通过利用多个 CPU 内核更快地生成条形码,从而提高应用程序性能并缩短大规模条形码任务的处理时间。

设置过多的并行线程会对性能产生负面影响吗?

是的,设置过多的并行线程会导致资源争用和开销增加,从而可能降低性能。重要的是要找到与系统能力相匹配的平衡配置。

在选择并行线程数量时应考虑哪些因素?

考虑因素包括可用 CPU 内核的数量、系统的工作量以及您的 BarCode 生成任务的性质。最好尝试不同的设置,以找到最佳配置。

IronBarcode 中是否有并行线程的默认设置?

IronBarcode 可能有并行线程的默认设置,但建议根据您的具体应用需求自定义此设置,以达到最佳性能。

IronBarcode 如何处理线程管理?

IronBarcode 利用 .NET 的线程功能管理并行处理,允许开发人员指定线程数量,以有效优化性能。

能否在执行过程中动态更改并行线程的数量?

不建议在执行过程中动态更改并行线程数,因为这会导致不一致并影响性能稳定性。最好在启动 BarCode 生成流程之前设置配置。

在条码生成中设置最大并行线程有哪些常见用例?

常见的使用案例包括需要高速生成 BarCode 的应用程序,如库存管理系统、零售点销售系统以及对快速处理大量条形码至关重要的物流应用程序。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

准备开始了吗?

Nuget Downloads 2,422,100版本:2026.9刚刚发布

立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 "IronBarcode"
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并将 IronBarCode 解压到解决方案目录中的 ~/Libs 等位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择浏览,"IronBarcode.dll"

许可 $999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户