IronBarcode 操作指南 异步和多线程 如何在 C# 中使用 IronBarcode 实现异步和多线程 Hairil Hasyimi Bin Omar 已更新:七月 22, 2025 下载 IronBarcode NuGet 下载 DLL 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English 术语异步和多线程操作经常被混淆。 两种方法旨在通过优化系统资源利用率和减少运行时间来提高程序性能和效率。然而,它们在方法,机制和预期用例方面有所不同。 IronBarcode支持这两种方法。 本文探讨了它们之间的区别以及如何使用IronBarcode实现它们。 快速入门:异步和多线程条码阅读示例 使用这个单行示例即可立即开始使用IronBarcode。 它展示了如何轻松结合异步读取和多线程选项,以最少的设置平行扫描多个条码图像。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronBarcode PM > Install-Package BarCode 复制并运行这段代码。 var results = await IronBarCode.BarcodeReader.ReadAsync(imagePaths, new IronBarCode.BarcodeReaderOptions { Multithreaded = true, MaxParallelThreads = 4, ExpectMultipleBarcodes = true }); 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronBarcode,免费试用! 免费试用30天 最小工作流程(5 个步骤) 下载用于异步和多线程支持的 C# 库 使用ReadAsync和ReadPdfAsync方法可以从图像和 PDF 中异步读取条形码。 将Multithreaded属性设置为"true"即可启用多线程。 使用MaxParallelThreads属性指定并行线程数 查看正常、异步和多线程条形码读取之间的性能比较 异步读取条码示例 我们先来了解一下什么是异步读取以及它如何为用户带来好处。 异步读取使长期或阻塞操作能够进行而不会阻塞主线程的执行。 在C#中,用户可以使用async和await关键字与支持异步功能的方法。 这不会创建额外的线程,而是释放当前线程。 当主线程启动和管理任务时,它不需要专门用于单个任务。 当异步方法需要其参与时,会召唤主线程,从而在不需要的时候释放其去处理其他任务,例如I/O绑定任务如读写文件或进行网络请求。 以条码读取为例。 在这种情况下,涉及的步骤将是: 读取文件 应用读取选项 解码条码 在"读取文件"步骤中,主任务可以被释放。 使用ReadAsync和ReadPdfAsync方法分别为图像和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方法。 用户可以指定图像路径以及读取选项。 这种异步操作的方法也可用于读取PDF文档中的条码,称为ReadPdfAsync,它是同一类的一部分。 多线程读取条码示例 与异步操作不同,多线程允许一个进程同时在多个线程中执行。 这意味着多线程将任务分配给多个线程,从而实现并发执行,而不是在单个线程中顺序执行一个进程。 然而,要使多线程运行,一台机器必须有多个CPU核心,因为这些核心用于独立执行线程。 与异步操作类似,多线程旨在提高应用程序的性能和响应能力。 在IronBarcode中,通过设置Multithreaded属性并使用BarcodeReaderOptions中的MaxParallelThreads指定并行执行的最大核心数来启用多线程。 MaxParallelThreads 的默认值为 4,可以根据可用 CPU 核心进行调整。 请注意要查找可用核心:任务管理器 -> 性能选项卡 -> 单击 CPU。 "核心数"字段显示计数。 :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 性能比较  #### 示例图像 正常阅读 异步读取 多线程读取(4 核) 1.75秒 1.67秒 1.17秒 然而,这两种操作服务于不同的目的和方法。 因此,需要用户根据他们正在构建的应用程序确定哪种方法更适合。 最后,可能会出现单个文档上有多个条码的情况。 有关更多信息,请参阅[读取多个条码](/csharp/barcode/how-to/read-multiple-barcodes/)指南。 如需了解更多信息,请访问["读取多个条形码"](/csharp/barcode/how-to/read-multiple-barcodes/)指南。 常见问题解答 如何在 C# 中实现异步条形码读取? 您可以通过使用 IronBarcode 的 ReadAsync 和 ReadPdfAsync 方法在 C# 中实现异步条形码读取。这些方法允许从图像和 PDF 读取条形码,而不会阻塞主线程。 启用条形码处理多线程的步骤是什么? 要启用条形码处理多线程,请将 Multithreaded 属性设置为 true 并配置 MaxParallelThreads 属性,以高效利用多个 CPU 核心。IronBarcode 支持这些配置。 使用异步操作读取条形码有什么优点? 在条形码读取中使用异步操作允许任务继续进行而不会阻塞主线程,从而增强应用程序的响应能力,特别是在 I/O 绑定操作中。IronBarcode 的异步方法通过使用 async 和 await 关键字来促进这一点。 多线程如何提高条形码读取性能? 多线程通过允许任务在多个 CPU 核心上并发执行,从而加快处理速度,提高条形码读取性能。这对于 CPU 绑定任务尤其有利。 多线程条形码读取中使用的默认线程数是多少? 在 IronBarcode 中,默认情况下用于多线程条形码读取的线程数是 4,可以根据您的 CPU 能力通过 MaxParallelThreads 属性进行调整。 在哪里可以下载支持异步和多线程条形码读取的 C# 库? 您可以从 .NET 包管理器 NuGet 下载支持异步和多线程条形码读取的 C# 库,网址为 https://nuget.org/packages/IronPdf/。该库提供异步和多线程操作的功能。 条形码读取可以在所有系统上使用多线程吗? 在具有多个 CPU 核心的系统上可以使用多线程,因为这些核心可以实现线程的并发处理。IronBarcode 的多线程功能利用了这些系统。 如何比较正常、异步和多线程条形码读取的性能? 性能比较表明,正常读取速度最慢,异步读取速度更快,多线程读取由于利用了多个 CPU 核心而最快。IronBarcode 提供这些读取选项以增强性能。 在文档中读取多个条形码的最佳方法是什么? IronBarcode 提供了全面支持读取文档中的多个条形码。有关详细的指导,请参考文档 /csharp/barcode/how-to/read-multiple-barcodes/。 Hairil Hasyimi Bin Omar 立即与工程团队聊天 软件工程师 如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。 准备开始了吗? Nuget 下载 1,979,979 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,979,979 查看许可证