如何在 C# 中使用非同步和多執行緒

How to Use Async and Multithread

This article was translated from English: Does it need improvement?
Translated
View the article in English

術語AsyncMultithreading操作經常被混淆。 這兩種方法都旨在通過優化系統資源利用率和減少運行時間來提高程式的性能和效率。然而,它們在方法、機制和預期使用案例上有所不同。 IronBarcode 支援這兩種方法。 這篇文章探討了它們之間的區別以及如何使用 IronBarcode 實現它們。

快速入門:異步與多執行緒條碼讀取範例

使用這個簡單的例子立即開始使用 IronBarcode。 它展示了如何輕鬆結合異步讀取和多執行緒選項,以最少的設定並行掃描多個條碼圖片。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var results = await IronBarCode.BarcodeReader.ReadAsync(imagePaths, new IronBarCode.BarcodeReaderOptions { Multithreaded = true, MaxParallelThreads = 4, ExpectMultipleBarcodes = true });
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載支援異步和多執行緒的 C# 庫
  2. 使用ReadAsyncReadPdfAsync方法從圖片和 PDF 異步讀取條碼
  3. 設置Multithreaded屬性為'true'以啟用多執行緒
  4. 使用MaxParallelThreads屬性指定並行執行緒數
  5. 檢查普通、異步和多執行緒條碼讀取之間的性能比較


異步讀取條碼範例

讓我們首先了解什麼是異步讀取以及它如何使用戶受益。 異步讀取允許長時間或阻塞操作在不阻塞主執行緒執行的情況下繼續進行。 在 C# 中,用戶可以對支援異步功能的方法使用asyncawait關鍵字。 這將不會創建額外的執行緒,而是釋放當前執行緒。 雖然仍然需要主執行緒來啟動和管理任務,但它不必專門用於單一任務。 當異步方法需要主執行緒參與時,主執行緒會被召喚,從而在不需要時可以處理其他任務:例如 I/O 相關任務,如讀取/寫入文件或發送網路請求。

讓我們考慮條碼讀取作為一個例子。 在這種情況下,涉及的步驟將是:

  • 讀取文件
  • 應用讀取選項
  • 解碼條碼

在“讀取文件”步驟期間,主任務可以被釋放。

使用ReadAsyncReadPdfAsync方法分別從圖片和 PDF 文檔中異步讀取條碼。

:path=/static-assets/barcode/content-code-examples/how-to/async-multithread-async.cs
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
$vbLabelText   $csharpLabel

從上面的代碼片段中,我們已經實例化了一個圖片路徑列表以便由 IronBarcode 來異步讀取。 要讀取圖片,您可以使用BarcodeReader類中的ReadAsync方法。 然後用戶可以指定 imagePaths 以及讀取選項。

這種方法也可以用來在 PDF 文檔中讀取條碼,稱為ReadPdfAsync,是相同類別的一部分。

在多執行緒中讀取條碼範例

與異步操作不同,多執行緒允許一個進程在多個執行緒中同時執行。 這意味著,不是順序地在單一執行緒中執行進程,多執行緒將任務細分為多個執行緒,允許並發執行。 然而,為了多執行緒運行,機器必須有多個 CPU 核心,因為這些核心用於獨立執行執行緒。 與異步操作類似,多執行緒也旨在提高應用程序的性能和響應能力。

在 IronBarcode 中,通過設置Multithreaded屬性並使用MaxParallelThreads在 BarcodeReaderOptions 中指定並發執行所需的最大核心數來啟用多執行緒。 MaxParallelThreads的默認值是 4,可以根據可用的 CPU 核心進行調整。

請注意要查找可用核心數量:任務管理器 -> 性能選項卡 -> 單擊 CPU。 'Cores'字段顯示計數。
### 性能比較

:path=/static-assets/barcode/content-code-examples/how-to/async-multithread-multithread.cs
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
$vbLabelText   $csharpLabel

現在,讓我們讀取以下兩個圖片,並比較正常、異步和多執行緒操作的讀取時間。

樣本圖片

![圖片1](/static-assets/barcode/how-to/async-multithread/sample1.webp)
![圖片2](/static-assets/barcode/how-to/async-multithread/sample2.webp)
正常讀取 異步讀取 多執行緒讀取(4 核心)
01.75秒 01.67秒 01.17秒

從比較表中可以看出,一旦實現了異步和多執行緒讀取,性能顯著提高。

然而,這兩個操作服務於不同的目的和方法。 因此,用戶需要確定哪種方法更適合他們正在建設的應用程序。 最後,可能會有多個條碼存在於同一文檔上的情況。

如需更多資訊,請訪問讀取多個條碼指南。 For more information, visit the Read Multiple Barcodes guide.

常見問題解答

我如何在 C# 中實現異步條碼讀取?

您可以使用 IronBarcode 的 ReadAsyncReadPdfAsync 方法在 C# 中實現異步條碼讀取。這些方法允許從圖片和 PDF 中讀取條碼而不阻塞主線程。

啟用條碼處理的多線程有哪些步驟?

若要啟用條碼處理的多線程,將 Multithreaded 屬性設置為 true 並配置 MaxParallelThreads 屬性以有效利用多個 CPU 核心。IronBarcode 支持這些配置。

使用異步操作進行條碼讀取有哪些優勢?

條碼讀取中的異步操作允許任務進行而不阻塞主線程,提高應用程式響應性,尤其是在 I/O 綁鉤操作中。IronBarcode 的異步方法透過使用 asyncawait 關鍵字來促進這一點。

多線程如何提高條碼讀取性能?

多線程透過允許在多個 CPU 核心上並行執行任務來提高條碼讀取性能,從而加快處理速度。這對於 CPU 綁鉤任務尤其有益。

在多線程條碼讀取中使用的默認線程數是多少?

在 IronBarcode 中,用於多線程條碼讀取的默認線程數為 4,您可以根據 CPU 的能力透過 MaxParallelThreads 屬性進行調整。

我可以在哪裡下載支援異步和多線程條碼讀取的 C# 函式庫?

您可以從 .NET 包管理器 NuGet 的 https://nuget.org/packages/IronPdf/ 下載支援異步和多線程條碼讀取的 C# 函式庫。此函式庫提供異步和多線程操作的功能。

條碼讀取的多線程功能可以在所有系統上使用嗎?

多線程可以在具有多個 CPU 核心的系統上使用,因為這些核心支持線程的並行處理。IronBarcode 的多線程功能能夠充分利用這樣的系統。

我如何比較正常、異步和多線程條碼讀取的性能?

性能比較顯示,正常讀取是最慢的,異步讀取更快,多線程讀取最快,因為利用了多個 CPU 核心。IronBarcode 提供這些讀取選項以提升性能。

閱讀文檔中的多個條碼最好的方法是什麼?

IronBarcode 提供全面支援以閱讀文檔中的多個條碼。有關詳細指引,請參考文檔 /csharp/barcode/how-to/read-multiple-barcodes/。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布