按顺序处理大量文档或图像可能非常耗时,从而在高容量应用程序中造成瓶颈。 使用并行线程可以让应用程序同时处理多张图像,从而显著缩短整体执行时间。在本代码示例中,我们将演示如何配置 IronBarcode 以使用多线程来加快批量条形码读取速度。
使用并行线程读取条形码的5步指南
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。