设置最大并行线程
按顺序处理大量文档或图像可能非常耗时,从而在高容量应用程序中造成瓶颈。 使用并行线程可以让应用程序同时处理多张图像,从而显著缩短整体执行时间。在本代码示例中,我们将演示如何配置 IronBarcode 以使用多线程来加快批量条形码读取速度。
使用并行线程读取条形码的五步指南
using IronBarCode;using System.Threading.Tasks;List<string> imagePaths = new List<string>() { "sample_a.png", "sample_b.png" };BarcodeReaderOptions options = new BarcodeReaderOptions(){ Multithreaded = true, MaxParallelThreads = 4 };BarcodeResults results = BarcodeReader.Read(imagePaths, options);
代码解释
首先,导入 IronBarcode 库和线程命名空间,并定义一个 List<string> 变量,其中包含待处理图像的文件路径。 在上面的示例中,"sample_a.png"和"sample_b.png"已准备好进行扫描。
接下来,实例化一个 BarcodeReaderOptions 对象以配置扫描行为。 Multithreaded 此外,将 ParallelProcessing 属性设置为 true 可启用并行处理。 Multithreaded MaxDegreeOfParallelism 属性也设置为 4,指示读者在平衡性能与系统资源使用的同时,最多使用 4 个并发线程。 MaxParallelThreads MaxParallelThreads Read BarcodeReader.Read
最后,调用 ReadBarcodes,同时传入图像路径列表和配置选项,以获取 BarcodeResults。

