如何使用异步和多线程
条款 异步 和 多线程 操作经常被混淆。这两种方法都旨在通过优化系统资源利用率和减少运行时间来提高程序性能和效率。然而,它们在方法、机制和预期用例上都有所不同。IronBarcode 支持这两种方法。本文将探讨它们之间的区别以及如何使用 IronBarcode 实现它们。
如何使用异步和多线程
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronBarcode 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变。
Install-Package BarCode
考虑安装 IronBarcode DLL 直接。下载并手动安装到您的项目或GAC表单中: IronBarCode.zip
手动安装到你的项目中
下载DLL异步读取条形码 示例
让我们先来了解一下什么是异步读取以及它如何为用户带来好处。异步读取可以在不阻塞主线程执行的情况下进行长操作或阻塞操作。在 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;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
#endregion
public class async_multithread_async
{
public async Task codeAsync()
{
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
#End Region
Public Class async_multithread_async
Public Async Function codeAsync() As Task
Dim imagePaths As New List(Of String)() From {"image1.png", "image2.png"}
' Barcode reading options
Dim options As New BarcodeReaderOptions() With {.ExpectMultipleBarcodes = True}
' Read barcode using Async
Dim 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
方法。然后,用户可以指定图像路径以及读取选项。
这种用于异步操作的方法也可用于读取 PDF 文档中的条形码,称为 "ReadPdfAsync",它也是该类的一部分。
在多线程示例中读取条形码
与异步操作不同,多线程允许单个进程在多个线程中同时执行。这意味着,多线程不是在单线程中顺序执行进程,而是将任务分配给多个线程,从而实现并发执行。不过,要实现多线程功能,机器必须有多个 CPU 内核,因为这些内核用于独立执行线程。与异步操作类似,多线程旨在提高应用程序的性能和响应速度。
在 IronBarcode 中,通过设置Multithreaded属性启用多线程,并在条码阅读器选项中使用MaxParallelThreads指定并发执行的最大内核。MaxParallelThreads 的默认值为 4,可根据可用的 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
性能比较
现在,让我们阅读下面两张图片,比较正常、异步和多线程操作的读取时间。
图片样本
正常阅读 | 异步读取 | 多线程读取(4 个内核) |
---|---|---|
01.75 秒 | 01.67 秒 | 01.17 秒 |
从比较表中可以看出,一旦实现异步和多线程读取,性能就会显著提高。不过,这两种操作的目的和方法各不相同。因此,用户需要确定哪种方法更适合他们正在构建的应用程序。
最后,在某些情况下,单个文档上可能会出现多个条形码。更多信息,请访问 读取多个条形码 指导。