設置最大平行執行緒數量
依序處理大量文件或圖片可能耗時甚久,進而在高流量應用程式中造成效能瓶頸。 透過使用並行執行緒,應用程式可同時處理多張圖像,大幅縮短整體執行時間。在此程式碼範例中,我們將示範如何設定 IronBarcode 以使用多執行緒,從而加快批次條碼讀取速度。
使用並行執行緒讀取 BarCode 的 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。

