如何使用异步和多线程
条款 异步 和 多线程 操作经常被混淆。 这两种方法都旨在通过优化系统资源利用和减少运行时间来提高程序的性能和效率。然而,它们在方法、机制和预期用例上有所不同。 IronBarcode支持这两种方法。 本文探讨了它们之间的差异以及如何使用IronBarcode来实现它们。
如何使用异步和多线程
开始使用 IronBarcode
立即在您的项目中开始使用IronBarcode,并享受免费试用。
异步读取条形码示例
让我们首先了解什么是异步阅读以及它如何惠及用户。 异步读取使得长时间或阻塞操作可以进行,而不会阻塞主线程的执行。 在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
从上面的代码片段中,我们已经实例化了一个图像路径列表,以便由IronBarcode异步读取。 要读取图片,您可以使用 BarcodeReader 类中的 ReadAsync
方法。 用户可以指定 imagePaths 以及阅读选项。
此异步操作的方法也可用于读取 PDF 文档中的条形码,称为 ReadPdfAsync
,属于同一类。
在多线程中读取条形码示例
与异步操作不同,多线程允许单个进程同时在多个线程中执行。 这意味着多线程不是在单一线程中顺序执行一个进程,而是将任务分配给多个线程,实现并发执行。 然而,要实现多线程功能,机器必须具有多个CPU核心,因为这些核心被用来独立执行线程。 与异步操作类似,多线程旨在提高应用程序的性能和响应性。
在IronBarcode中,通过设置Multithreaded属性并使用MaxParallelThreads在BarcodeReaderOptions中指定最大核心数来启用多线程并发执行。 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 个内核) |
---|---|---|
1.75 秒 | 1.67 秒 | 1.17 秒 |
从比较表中可以明显看出,一旦实现了异步和多线程读取,性能显著提升。 然而,这两种操作服务于不同的目的和方法。 因此,用户需要确定哪种方法更适合他们正在构建的应用程序。
最后,可能会出现单个文件上有多个条形码的情况。 欲了解更多信息,请访问读取多个条形码指导。