設置最大平行執行緒數量
依序處理大量文件或圖像可能會耗時,並在高需求應用中造成瓶頸。 使用平行執行緒允許應用程式同時處理多個圖像,顯著減少整體執行時間。在本程式碼範例中,我們將示範如何配置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
最後,調用BarcodeResults。

