如何使用异步和多线程
術語非同步和多執行緒操作經常被混淆。 兩種方法都旨在通過優化系統資源使用和減少運行時間來提高程序性能和效率。然而,它們在方法、機制和預期用例上有所不同。 IronBarcode 支持這兩種方法。 本文探討了它們之間的差異以及如何使用IronBarcode來實現它們。
如何使用异步和多线程
開始使用 IronBarcode
立即在您的專案中使用IronBarcode,並享受免費試用。
異步讀取條形碼示例
讓我們開始了解什麼是異步讀取以及它如何對用戶有益。 異步讀取允許長時間或阻塞操作繼續進行,而不會阻塞主線程的執行。 在 C# 中,使用者可以使用支援非同步功能的方法搭配 async 和 await 關鍵字。 這不會創建額外的執行緒,而是釋放當前執行緒。 主執行緒仍然必須啟動和管理任務,但不需要專門用於單一任務。 當異步方法需要其參與時,主線程被召喚,當不需要時,它可以釋放來處理其他任務—例如 I/O 綁定任務,如讀取/寫入文件或進行網絡請求。
讓我們以條碼讀取為例。 在這種情況下,所涉及的步驟將是:
- 讀取檔案
- 應用閱讀選項
解碼條碼
在「讀取檔案」步驟中,可以釋放主任務。
使用
ReadAsync和ReadPdfAsync方法,分別非同步讀取影像和 PDF 文件中的條碼。
:path=/static-assets/barcode/content-code-examples/how-to/async-multithread-async.csusing 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從上面的程式碼片段中,我們已經實例化了一個圖像路徑列表,以便由IronBarcode異步讀取。 若要讀取圖像,可以使用BarcodeReader類別中的ReadAsync方法。 使用者可以指定 imagePaths 以及閱讀選項。
這種用於異步操作的方法也可以用來讀取 PDF 文件中的條碼,稱為 ReadPdfAsync,是同一個類的一部分。
在多執行緒中讀取條碼的示例
與異步操作不同,多線程允許單一進程同時在多個線程中執行。 這意味著多線程不是在單一線程中按順序執行一個過程,而是將任務分配到多個線程中,以實現並行執行。 然而,要讓多緒運作,機器必須擁有多個 CPU 核心,因為這些核心被用來獨立執行緒。 與異步操作類似,多線程旨在提高應用程序的性能和響應速度。
在 IronBarcode 中,通過設定Multithreaded屬性並使用 BarcodeReaderOptions 中的MaxParallelThreads來指定併發執行的最大核心數,以啟用多執行緒。 MaxParallelThreads 的預設值為 4,可以根據可用的 CPU 核心進行調整。
[{i:(要查找可用的內核:任務管理器 -> 性能選項卡 -> 點擊 CPU。 「核心」欄位顯示計數。)}]
:path=/static-assets/barcode/content-code-examples/how-to/async-multithread-multithread.csusing 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性能比較
現在,讓我們閱讀以下兩張圖片,並比較正常、非同步和多執行緒操作的讀取時間。
範例圖片


| Normal Read | Asynchronous Read | Multithreaded Read (4 cores) |
|---|---|---|
| 01.75 second | 01.67 second | 01.17 second |
從比較表中可以明顯看出,一旦實施了異步和多線程讀取,性能顯著提高。 然而,這兩種操作具有不同的目的和方法。 因此,使用者需要決定哪種方法更適合他們正在建構的應用程式。
最後,單一文件上可能會出現多個條碼的情況。 如需更多資訊,請參閱讀取多個條碼指南。




