如何设置最大并行线程数
读取大量条形码时,依赖单线程进程可能会造成性能瓶颈并限制可扩展性。 但是,使用并行线程可以让应用程序同时处理多个图像,从而有效地提高总处理能力,并大幅缩短完成批处理作业所需的时间。
为这些线程设置最大限制是优化性能的有效方法。 它通过在处理器核心之间平衡工作负载,确保应用程序充分利用硬件的潜力。 这种方法可以最大限度地提高效率,在保持应用程序流畅运行的同时,提供尽可能快的结果。
IronBarcode 提供了一种简单的方法来控制此限制,从而确保实现最佳的机器性能。 以下部分演示如何轻松设置这些线程限制。
开始使用 IronBarcode
如何在 C# 中设置最大并行线程数
- 下载 IronBarcode C# 库,设置读取条形码的最大并行线程数。
- 从指定文件夹加载条形码
- 实例化一个新的
BarcodeReaderOptions对象 - 将
Multithreaded设置为 true,并为MaxParallelThreads提供一个整数值。 - 使用
BarcodeResults打印并显示结果
设置最大并行线程数
在这个例子中,我们将使用大量的条形码图像来说明使用多线程进程而不是单线程进程的可扩展性和效率。 您可以点击此处下载图片文件夹。
要配置 IronBarcode 使用多个线程,首先需要实例化一个新的BarcodeReaderOptions对象,并将Multithreaded设置为 true。 之后,通过赋予一个整数值来设置MaxParallelThreads属性。 默认情况下, MaxParallelThreads设置为 4。
配置完成后,从文件夹中导入大量条形码图像。 然后,使用循环,通过Read方法读取条形码图像目录,传递文件路径和配置的BarcodeReaderOptions 。 最后,通过访问BarcodeResults显示条形码的值和类型。
:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread.csusing 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);
foreach (var result in results)
{
// Show the type and value for every barcode found
Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");
}
}
输出

如控制台输出所示,它会显示每个对应图像的条形码值和类型。
设置合适的最大并行线程数
当Multithreaded属性设置为 true 时, MaxParallelThreads属性的默认值为 4。虽然MaxParallelThreads的整数值没有硬性限制,但将其值设置得高于硬件的逻辑核心容量实际上会导致性能下降。 这是因为处理器无法处理过多的上下文切换,这可能会导致性能开销而不是速度提升。 因此, MaxParallelThreads的正确值取决于计算机的规格,开发人员应该进行测试,以找到适合其环境的最佳值。
在这个例子中,我们将展示与上面相同的多线程场景,但会设置一个计时器,将默认值 4 与使用Environment.ProcessorCount来利用所有可用线程进行比较。 在本例中,我们使用一台具有 32 个逻辑处理器的计算机,因此MaxParallelThreads将设置为 32。
:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread-performance.csusing 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);
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");输出
4线程处理时间

该过程的处理时间为 84 秒。
处理时间与环境处理器数量

如您所见,此操作的处理时间为 53 秒,这比仅使用四个线程运行它要快得多。 但是请注意,使用更多线程并不能保证性能提升,因为性能取决于主机处理器。 一般经验法则是使用可用处理器的最大数量减一,以确保仍然有一个线程可用于其他系统操作。
项目环境必须配置为允许多线程。 否则,将Multithreaded设置为true并增加MaxParallelThreads值并不会提高进程速度,反而可能会降低进程速度。
常见问题解答
在 IronBarcode 中设置最大并行线程数的目的是什么?
设置最大并行线程数可以有效利用系统资源,从而优化条形码生成性能,尤其是在批量处理大量条形码时。
如何在IronBarcode中配置最大并行线程数?
您可以通过在 C# 代码中使用适当的方法来配置 IronBarcode 中的最大并行线程数,从而设置条形码生成任务所需的线程数。
为什么优化批量条形码创建的性能很重要?
优化批量条形码创建的性能,可确保流程高效快速,减少生成大量条形码所需的时间和资源,这对于高吞吐量需求的应用至关重要。
在IronBarcode中使用并行处理有哪些好处?
IronBarcode 的并行处理功能利用多个 CPU 核心,可以更快地生成条形码,从而提高应用程序性能,并减少大规模条形码任务的处理时间。
设置过多的并行线程会对性能产生负面影响吗?
是的,设置过多的并行线程会导致资源争用和开销增加,从而可能降低性能。找到一个与系统性能相匹配的平衡配置至关重要。
选择并行线程数时应考虑哪些因素?
需要考虑的因素包括可用 CPU 核心数、系统负载以及条形码生成任务的性质。最好尝试不同的设置,找到最佳配置。
IronBarcode 中并行线程是否有默认设置?
IronBarcode 可能对并行线程有默认设置,但建议根据您的具体应用程序需求自定义此设置,以获得最佳性能。
IronBarcode 如何处理线程管理?
IronBarcode 利用 .NET 的线程功能来管理并行处理,允许开发人员指定线程数以有效地优化性能。
我可以在执行过程中动态更改并行线程数吗?
不建议在执行过程中动态更改并行线程数,因为这可能会导致结果不一致并影响性能稳定性。最好在开始条形码生成过程之前设置好配置。
条形码生成中设置最大并行线程数的常见应用场景有哪些?
常见用例包括需要高速生成条形码的应用,例如库存管理系统、零售销售点系统和物流应用,在这些应用中,快速处理大量条形码至关重要。






