How to use Async and Multithreading

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

在不斷發展的軟體開發領域中,高效處理大量文本數據始終是一個關鍵挑戰。 在本文中,我們探討了 IronOCR 和 Tesseract 中異步支持與多執行緒的動態協同效應。 異步程式設計引入了一種非阻塞的範式,確保我們的應用程式在執行 OCR 任務時保持靈活和響應。 同時,我們深入研究多執行緒領域,揭示平行處理潛力,顯著提升文本識別操作的性能。 加入我們,了解這些技術的整合,讓開發人員能提升 OCR 驅動應用程式的效率和響應能力。

快速入門:使用 ReadAsync 輕鬆使用異步 OCR

這裡有一個簡單的入門方法:使用 IronTesseract 的 ReadAsync 方法來執行 OCR,而不會阻塞您的主執行緒。 非常適合快速為應用程式添加響應的、非阻塞的 OCR 功能。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    var result = await new IronOcr.IronTesseract().ReadAsync("image.png");
  3. Deploy to test on your live environment

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

最小工作流程 (5 步驟)

  1. 下載支持 Tesseract 且具有異步和多執行緒功能的 C# 程式庫
  2. 利用 IronOCR 管理的多執行緒
  3. 準備為 PDF 文件和圖像進行閱讀
  4. 運用 OcrReadTask 對象利用異步並發性優勢
  5. 使用 ReadAsync 方法來簡化使用

瞭解多執行緒

在 IronOCR 中,無縫的多執行緒增強了影像處理和 OCR 閱讀的效率,消除了開發人員使用專門 API 的需要。 IronTesseract 自動利用所有可用的多核執行緒,優化系統資源以快速和響應的執行 OCR。 這種內在的多執行緒不僅簡化了開發,還顯著提升了性能,展示了將平行性熟練整合到 OCR 工作流程中的精巧。

以下是 C# 中可能的多執行緒閱讀外觀:

:path=/static-assets/ocr/content-code-examples/how-to/async-simple-multithreading.cs
using IronOcr;
using System;

var ocr = new IronTesseract();

using (var input = new OcrPdfInput(@"example.pdf"))
{
    var result = ocr.Read(input);
    Console.WriteLine(result.Text);
};
Imports IronOcr
Imports System

Private ocr = New IronTesseract()

Using input = New OcrPdfInput("example.pdf")
	Dim result = ocr.Read(input)
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

了解異步支持

在光學字符識別 (OCR) 領域中,異步程式設計或“異步”在優化性能方面發揮著關鍵作用。 異步支持允許開發者在不阻塞主執行緒的情況下執行 OCR 任務,確保應用程式保持響應。想像一下當具有異步支持系統在進行 OCR 操作時仍能繼續處理其他任務。

在本節中,我們將深入了解如何在 IronOCR 中簡便整合異步支持,展示如何讓您的 OCR 服務成為非阻塞。

使用 OcrReadTask 對象

使用 IronOCR 時,利用 OcrReadTask 對象證明在增強 OCR 流程中的控制力和靈活性中扮演著寶貴的角色。 這些對象封裝了 OCR 操作,允許開發者高效管理文本識別任務。 本節提供了在您的 IronOCR 工作流程中引入 OcrReadTask 對象的示例,展示如何利用這些資源來發起和優化 OCR 任務。 無論您是在編排複雜的文件處理還是在微調您的 OCR 驅動應用程式的響應能力,有效使用 OcrReadTask 對象都有助於最大化 IronOCR 的功能。

:path=/static-assets/ocr/content-code-examples/how-to/async-ocrtask.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();

OcrPdfInput largePdf = new OcrPdfInput("chapter1.pdf");

Func<OcrResult> reader = () =>
{
    return ocr.Read(largePdf);
};

OcrReadTask readTask = new OcrReadTask(reader.Invoke);
// Start the OCR task asynchronously
readTask.Start();

// Continue with other tasks while OCR is in progress
DoOtherTasks();

// Wait for the OCR task to complete and retrieve the result
OcrResult result = await Task.Run(() => readTask.Result);

Console.Write($"##### OCR RESULTS ###### \n {result.Text}");

largePdf.Dispose();
readTask.Dispose();

static void DoOtherTasks()
{
    // Simulate other tasks being performed while OCR is in progress
    Console.WriteLine("Performing other tasks...");
    Thread.Sleep(2000); // Simulating work for 2000 milliseconds
}
Imports Microsoft.VisualBasic
Imports IronOcr

Private ocr As New IronTesseract()

Private largePdf As New OcrPdfInput("chapter1.pdf")

Private reader As Func(Of OcrResult) = Function()
	Return ocr.Read(largePdf)
End Function

Private readTask As New OcrReadTask(AddressOf reader.Invoke)
' Start the OCR task asynchronously
readTask.Start()

' Continue with other tasks while OCR is in progress
DoOtherTasks()

' Wait for the OCR task to complete and retrieve the result
Dim result As OcrResult = Await Task.Run(Function() readTask.Result)

Console.Write($"##### OCR RESULTS ###### " & vbLf & " {result.Text}")

largePdf.Dispose()
readTask.Dispose()

'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'static void DoOtherTasks()
'{
'	' Simulate other tasks being performed while OCR is in progress
'	Console.WriteLine("Performing other tasks...");
'	Thread.Sleep(2000); ' Simulating work for 2000 milliseconds
'}
$vbLabelText   $csharpLabel

使用異步方法

ReadAsync() 提供了一個簡單易用的機制來異步啟動 OCR 操作。 開發者可以輕鬆將異步 OCR 整合到他們的應用程式中,而無需複雜的執行緒或任務管理。 此方法通過避免主執行緒被阻塞 OCR 任務的負擔,確保應用程式保持響應性和靈活性。

:path=/static-assets/ocr/content-code-examples/how-to/async-read-async.cs
using IronOcr;
using System;
using System.Threading.Tasks;

IronTesseract ocr = new IronTesseract();

using (OcrPdfInput largePdf = new OcrPdfInput("PDFs/example.pdf"))
{
    var result = await ocr.ReadAsync(largePdf);
    DoOtherTasks();
    Console.Write($"##### OCR RESULTS ###### " +
                $"\n {result.Text}");
}

static void DoOtherTasks()
{
    // Simulate other tasks being performed while OCR is in progress
    Console.WriteLine("Performing other tasks...");
    System.Threading.Thread.Sleep(2000); // Simulating work for 2000 milliseconds
}
Imports Microsoft.VisualBasic
Imports IronOcr
Imports System
Imports System.Threading.Tasks

Private ocr As New IronTesseract()

Using largePdf As New OcrPdfInput("PDFs/example.pdf")
	Dim result = Await ocr.ReadAsync(largePdf)
	DoOtherTasks()
	Console.Write($"##### OCR RESULTS ###### " & $vbLf & " {result.Text}")
End Using

'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'static void DoOtherTasks()
'{
'	' Simulate other tasks being performed while OCR is in progress
'	Console.WriteLine("Performing other tasks...");
'	System.Threading.Thread.Sleep(2000); ' Simulating work for 2000 milliseconds
'}
$vbLabelText   $csharpLabel

結論

總結來說,在 IronOCR 中利用多執行緒被證明是優化 OCR 任務的遊戲改變者。 IronOCR 的內置多執行緒能力,結合友好的方法如 ReadAsync(),簡化了大量文本數據的處理。 這種協同作用確保您的應用程式保持響應性和高效,使 IronOCR 成為創建高性能軟體解決方案的強大工具,具備精簡的文本識別能力。

常見問題解答

非同步程式設計如何提升OCR任務的效能?

IronOCR 中的非同步程式設計允許 OCR 任務在不阻塞主應用程式執行緒的情況下運行。這確保了應用程式在背景處理 OCR 操作時保持回應。

多執行緒在OCR過程中扮演什麼角色?

IronOCR 中的多執行緒技術能夠跨多個核心進行並行處理,從而優化系統資源並顯著提高文字辨識操作的效能。

如何在 Tesseract 中開始使用非同步和多執行緒?

要開始在 Tesseract 中使用非同步和多線程,請下載支援這些特性的 C# 程式庫。 IronOCR 可以無縫集成,讓您能夠使用ReadAsync()等方法高效地管理 OCR 任務。

OcrReadTask 物件是用來做什麼的?

IronOCR 中的OcrReadTask物件封裝了 OCR 操作,為開發人員提供了增強的控制和靈活性,以便有效率地管理文字辨識任務。

如何執行非阻塞式 OCR 操作?

在 IronOCR 中,您可以使用ReadAsync()方法執行非阻塞式 OCR 操作。這種方法允許 OCR 任務非同步運行,從而釋放主執行緒並保持應用程式的回應速度。

IronOCR 是否可以同時處理 PDF 和影像?

是的,IronOCR可以處理PDF和圖像進行文字辨識。它利用多線程和非同步功能高效地處理各種文件類型。

我是否需要專門的 API 才能在 IronOCR 中實現多線程?

不,您不需要專門的 API。 IronOCR 會自動管理多線程,利用所有可用核心來最佳化 OCR 任務效能。

ReadAsync() 方法對 OCR 應用程式有何好處?

IronOCR 中的ReadAsync()方法可讓開發人員非同步啟動 OCR 操作,確保應用程式即使在處理大量文字資料時也能保持敏捷性和回應性。

在OCR中同時使用非同步和多執行緒有哪些優點?

在 IronOCR 中同時使用非同步和多執行緒技術,可以有效地優化 OCR 任務。非同步技術確保任務無阻塞執行,而多執行緒技術則利用多個核心實現更快的處理速度。

IronOCR能否在多核心處理器上自動處理OCR任務?

是的,IronOCR 會自動利用所有可用核心執行 OCR 任務,優化處理速度和資源利用率,而無需開發人員進行明確管理。

IronOCR 是否完全相容於 .NET 10?

IronOCR 最新版本 2025.11 支援 .NET 10。您可以使用 NuGet (Install-Package IronOcr) 安裝該程式庫,並在 .NET 10 下執行ReadAsync()等非同步方法,無需特殊配置。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 5,044,537 | 版本: 2025.11 剛剛發布