依序處理大量文件或影像可能非常耗時,從而在高容量應用程式中造成瓶頸。 使用平行執行緒可以讓應用程式同時處理多張影像,從而顯著縮短整體執行時間。在本程式碼範例中,我們將示範如何配置 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。