如何使用异步和多线程

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

海瑞尔 哈西米 本 奥马尔

条款 异步多线程 操作经常被混淆。 这两种方法都旨在通过优化系统资源利用和减少运行时间来提高程序的性能和效率。然而,它们在方法、机制和预期用例上有所不同。 IronBarcode支持这两种方法。 本文探讨了它们之间的差异以及如何使用IronBarcode来实现它们。


开始使用 IronBarcode

立即在您的项目中开始使用IronBarcode,并享受免费试用。

第一步:
green arrow pointer

异步读取条形码示例

让我们首先了解什么是异步阅读以及它如何惠及用户。 异步读取使得长时间或阻塞操作可以进行,而不会阻塞主线程的执行。 在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
VB   C#

从上面的代码片段中,我们已经实例化了一个图像路径列表,以便由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
VB   C#

性能比较

现在,让我们阅读下面的两张图片,并比较普通、异步和多线程操作的阅读时间。

图片样本

图片 1
图片 2
正常阅读异步读取多线程读取(4 个内核)
1.75 秒1.67 秒1.17 秒

从比较表中可以明显看出,一旦实现了异步和多线程读取,性能显著提升。 然而,这两种操作服务于不同的目的和方法。 因此,用户需要确定哪种方法更适合他们正在构建的应用程序。

最后,可能会出现单个文件上有多个条形码的情况。 欲了解更多信息,请访问读取多个条形码指导。

Hairil related to 图片样本

海瑞尔 哈西米 本 奥马尔

软件工程师

像所有优秀的工程师一样,Hairil 是一个热衷学习的人。他正在精进自己的 C#、Python 和 Java 知识,并利用这些知识为 Iron Software 团队成员增添价值。Hairil 毕业于马来西亚的马来西亚工艺大学(Universiti Teknologi MARA),获得了化学与工艺工程学士学位,然后加入了 Iron Software 团队。