如何在C#中使用IronBarcode進行非同步和多執行緒操作
IronBarcode中的非同步和多執行緒優化條碼讀取性能的方式不同 - 非同步防止在I/O操作期間阻塞主執行緒,而多執行緒則在各個CPU核心上同時處理多個條碼。
開發者經常混淆Multithreading操作。 這兩種方法透過優化系統資源利用率和降低運行時間來提升程式的性能和效率。然而,它們的方式、機制和使用案例不同。 IronBarcode支援這兩種方法。 本文探討它們的差異及在IronBarcode中的實現方法。
使用這個一行範例即可立即開始使用IronBarcode。 它展示了如何輕鬆結合非同步讀取和多執行緒選項,以最少的設置並行掃描多個條碼圖像。
-
1Install IronBarcode with NuGet Package Manager
-
2複製並運行這段程式碼片段。
var results = await IronBarCode.BarcodeReader.ReadAsync(imagePaths, new IronBarCode.BarcodeReaderOptions { Multithreaded = true, MaxParallelThreads = 4, ExpectMultipleBarcodes = true });C# -
3部署以在您的實時環境中測試
今天就開始在您的專案中使用IronBarcode,透過免費試用
最小化工作流程(5步驟)
- 下載支援非同步和多執行緒的C#程式庫
- 使用
ReadAsync和ReadPdfAsync方法以非同步方式從圖像和PDF中讀取條碼 - 將Multithreaded屬性設置為'true'以啟用多執行緒
- 使用MaxParallelThreads屬性指定並行執行緒數量
- 檢查普通、非同步和多執行緒條碼讀取的性能比較
如何使用IronBarcode非同步讀取條碼?
非同步讀取允許長時間或阻塞操作在不阻塞主執行緒執行的情況下進行。 在C#中,使用支援非同步特性的await關鍵字。 這種方式不會建立額外的執行緒,而是釋放當前的執行緒。 雖然主執行緒啟動和管理任務,但它並不專用於單一任務。 當非同步方法需要主執行緒的參與時,主執行緒返回,使其在不需要時可以處理其他任務——尤其對於I/O限制的任務如讀寫文件或發起網路請求非常有用。
以條碼讀取為例。 這一過程涉及:
- 讀取文件
- 應用讀取選項
- 解碼條碼
在文件讀取期間,主任務可以被釋放。 這對於有多個圖像文件或大型PDF的情境有益,如我們在條碼讀取教程中所示範。
使用ReadPdfAsync方法分別讀取圖像和PDF文件中的條碼。 在實施非同步操作之前,請確保您已在項目中通過NuGet安裝IronBarcode。
using IronBarCode;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
List<string> imagePaths = new List<string>() { "image1.png", "image2.png" };
// Barcode reading options
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true
};
// Read barcode using Async
BarcodeResults asyncResult = await BarcodeReader.ReadAsync(imagePaths, options);
// Print the results to console
foreach (var result in asyncResult)
{
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks
Private imagePaths As New List(Of String)() From {"image1.png", "image2.png"}
' Barcode reading options
Private options As New BarcodeReaderOptions() With {.ExpectMultipleBarcodes = True}
' Read barcode using Async
Private asyncResult As BarcodeResults = await BarcodeReader.ReadAsync(imagePaths, options)
' Print the results to console
For Each result In asyncResult
Console.WriteLine(result.ToString())
Next result上面的程式碼片段會初始化一個List,以便IronBarcode非同步讀取圖像路徑。 要讀取圖像,可以使用BarcodeReader類。 指定imagePaths和讀取選項。 如需進階的配置選項,請參閱我們的條碼讀取器設置指南。
這種非同步操作方法也可以用於通過同一類的ReadPdfAsync在PDF文件中讀取條碼。 如需特定的PDF讀取配置,請參閱我們的PDF條碼讀取器設置指南。
Multithreaded
MaxParallelThreads
什麼時候應該選擇非同步讀取而非常規方法?
非同步讀取在多種情況下表現出色:
-
GUI應用程式:需要UI反應速度的Windows Forms或WPF應用程式。 非同步可防止在條碼掃描期間介面凍結。
-
Web應用程式:ASP.NET應用程式在不阻塞執行緒的情況下處理多個併發請求,特別是在處理上傳的條碼圖像時。
-
批次處理:多個條碼圖像或PDF的序列讀取,允許其它任務在I/O操作期間執行。
-
網路操作:從遠端資源或URL讀取條碼,如我們的非同步範例從URL讀取條碼。
為什麼非同步讀取能提高應用程式的響應能力?
非同步讀取透過在I/O約束操作期間釋放主執行緒來提高響應能力。 當IronBarcode從磁碟讀取圖像文件或處理PDF時,執行緒不會保持空閒。 相反,它會處理其他任務,如回應使用者輸入或處理請求。 這在以下情況特別明顯:
- 需要顯著載入時間的大型圖像文件
- 含有多個條碼的多頁面PDF
- 基於網路的圖像來源
- 在條碼檢測前需要圖像校正濾鏡的情境
使用非同步條碼讀取的常見陷阱是什麼?
實施非同步條碼讀取時,需注意以下常見問題:
-
死鎖:在UI上下文中避免對非同步方法的
Wait()。 始終在呼叫鏈中使用await。 -
異常處理:將非同步調用包在try-catch塊中,因為在非同步方法中的異常可能不會按預期傳播。
-
上下文切換:當不需要返回原始同步上下文時,考慮使用
ConfigureAwait(false)。 -
性能誤解:非同步不能加快單個操作; 它提高應用程式的響應能力。 對於多個圖像的速度提升,考慮使用多執行緒。
有關非同步相關問題的疑難排解,請參閱我們的條碼識別疑難排解指南。
如何啟用多執行緒條碼讀取?
與非同步操作不同,多執行緒能同時在多個執行緒上執行單個進程。 而不是在單個執行緒中順序執行,多執行緒將任務分配給多個執行緒以同時執行。 多執行緒需要多個CPU核心,因為這些核心獨立執行執行緒。 與非同步操作類似,多執行緒提高了應用程式的性能和響應能力。
在IronBarcode中,通過設置BarcodeReaderOptions中指定並行執行的最大核心數。 預設MaxParallelThreads值為4,可根據可用CPU核心調整。 如需最佳性能配置,請參閱我們的讀取速度選項指南。
using IronBarCode;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
List<string> imagePaths = new List<string>(){"test1.jpg", "test2.png"};
// Barcode reading options
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
Multithreaded = true,
MaxParallelThreads = 4,
ExpectMultipleBarcodes = true
};
// Read barcode with multithreaded enabled
BarcodeResults results = BarcodeReader.Read(imagePaths, options);
// Print the results to console
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks
Private imagePaths As New List(Of String)() From {"test1.jpg", "test2.png"}
' Barcode reading options
Private options As New BarcodeReaderOptions() With {
.Multithreaded = True,
.MaxParallelThreads = 4,
.ExpectMultipleBarcodes = True
}
' Read barcode with multithreaded enabled
Private results As BarcodeResults = BarcodeReader.Read(imagePaths, options)
' Print the results to console
For Each result In results
Console.WriteLine(result.ToString())
Next result我可以期望有多少性能提升?
我們來讀取兩個範例圖像,並比較普通、非同步和多執行緒操作的讀取時間。
範例圖像


| 普通讀取 | 非同步讀取 | 多執行緒讀取(4核心) |
|---|---|---|
| 01.75秒 | 01.67秒 | 01.17秒 |
比較顯示,非同步和多執行緒讀取帶來了顯著的性能提升。 多執行緒比普通讀取提高了大約33%,而非同步則提高了約5%。 然而,這些操作服務於不同的目的和方法。 選擇最適合您應用程式需求的方法。
性能提升取決於:
- 處理的圖像數量
- 圖像的複雜性和條碼質量
- 可用的CPU核心
- 其他系統資源
如在單個文件上存在多個條碼,查看讀取多個條碼指南。
什麼時候應該選擇多執行緒而非非同步操作?
在以下情況下選擇多執行緒:
- CPU密集型運算:處理涉及複雜運算,如複雜圖像濾鏡或高解析度圖像
- 批次處理:多個獨立圖像需要同時處理
- 多核心系統:部署環境提供多個可用的CPU核心
- 性能至關重要:原始處理速度超出資源效率
在以下情況下選擇非同步操作:
- I/O限制操作:大部分時間涉及讀取文件或等待網路回應
- UI應用程式:維持響應式使用者介面至為重要
- 資源有限:在有限CPU核心的系統上運行
- Web應用程式:高效處理多個併發請求
我如何確定最佳的MaxParallelThreads值?
最佳MaxParallelThreads值取決於多個因素:
- 可用CPU核心:從
Environment.ProcessorCount作為基準開始 - 工作負載型別:純條碼讀取用途時,使用可用核心的75%
- 系統資源:留有空間給操作系統和其他進程
- 測試結果:使用您的特定工作負載基準測試
這是找到最佳值的實用方法:
int optimalThreads = Math.Max(1, Environment.ProcessorCount - 1);net對於生產環境,監控性能並根據實際使用模式進行調整。 考慮對需要最大性能的Enterprise部署實施授權金鑰配置。
如需完整的API功能,請查閱IronBarcode API參考。
常見問題
條碼讀取時非同步和多執行緒有什麼區別?
使用IronBarcode,非同步操作在進行I/O任務(如文件讀取)時防止阻塞主執行緒,而多執行緒則同時在多個CPU核心上處理多個條碼。非同步使用async/await關鍵字在長時間操作時釋放主執行緒,而多執行緒建立多個執行緒來平行處理條碼。
我如何在C#中實現非同步條碼讀取?
IronBarcode提供ReadAsync()和ReadPdfAsync()方法用於非同步條碼讀取。只需在C#中使用這些方法與async/await關鍵字,即可從圖片和PDF中讀取條碼而不阻塞主執行緒。這在處理多個圖片文件或大型PDF文件時特別有益。
如何啟用條碼處理的多執行緒?
要在IronBarcode中啟用多執行緒,請在BarcodeReaderOptions中將Multithreaded屬性設置為'true'。您還可以使用MaxParallelThreads屬性控制平行執行緒的數量,根據您的系統能力來優化性能。
我可以結合非同步和多執行緒以獲得最佳性能嗎?
是的,IronBarcode允許您結合兩種方法。您可以使用ReadAsync()並設定Multithreaded = true以啟用多執行緒,並在BarcodeReaderOptions中配置MaxParallelThreads。此組合提供非阻塞I/O操作和跨多個CPU核心的平行處理。
何時應使用非同步與多執行緒進行條碼讀取?
當處理I/O密集型任務(如讀取大型文件或進行網路請求)時,使用IronBarcode的非同步操作以防止UI卡頓。當需要同時處理多個條碼且有CPU資源可用時,使用多執行緒處理。對於最佳效果,建議在處理多個大型條碼圖像或PDF文件時結合使用兩種方法。
What factors affect the optimal 'MaxParallelThreads' setting in IronBarcode?
The optimal 'MaxParallelThreads' value is influenced by available CPU cores, workload type, overall system resources, and testing results. A practical starting point is to use one less than the total available CPU cores.
How does IronBarcode's async functionality improve application responsiveness?
Async functionality in IronBarcode increases application responsiveness by freeing the main thread during long I/O-bound barcode reading tasks, allowing the application to handle other processes concurrently, such as user interface updates.
What are the advantages of using multithreading in barcode reading?
Multithreading allows simultaneous processing of multiple images, which enhances performance in CPU-bound operations such as processing complex image filters or high-resolution images. This is beneficial in environments with multiple CPU cores.
How can I implement license key configuration for optimal performance in IronBarcode?
For enterprise deployments requiring maximum performance, consider implementing license key configuration. This can be done by following IronBarcode's documentation on license key settings to fully unlock features and optimize resource utilization.
What are the key differences between async and multithreading in IronBarcode?
Async in IronBarcode prevents blocking the main thread during I/O operations, ideal for file and network tasks, whereas multithreading divides tasks among threads for concurrent execution across CPU cores, improving performance in CPU-bound operations.
