How to use Async and Multithreading in C# | IronOCR

C# Async & Multithreading Support with IronOCR

IronOCR automatically handles multithreading across all CPU cores and provides async methods like ReadAsync() to perform OCR without blocking your main thread, ensuring responsive applications while processing large documents. For memory-constrained workloads, the MaxDegreeOfParallelism and MultiThreaded properties on IronTesseract cap how many pages are read at once.

Introduction

Processing large volumes of text data efficiently requires both accuracy and speed for OCR operations. This article covers async support and multithreading in IronOCR and Tesseract. Asynchronous programming enables non-blocking OCR execution, keeping applications responsive during text recognition tasks. Multithreading provides parallelism to significantly boost OCR performance. These techniques help developers improve the efficiency and responsiveness of OCR-powered applications.

Quickstart: Use ReadAsync for Effortless Async OCR

Use IronTesseract's ReadAsync method to perform OCR without blocking your main thread. This quickly adds responsive, non-blocking OCR to your application. It works particularly well when processing PDF documents or handling multiple image files simultaneously.

  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

How Does Multithreading Work in IronOCR?

IronOCR enhances image processing and OCR reading efficiency through seamless multithreading, requiring no specialized API from developers to get started. IronTesseract automatically leverages all available threads across multiple cores by default, optimizing system resources for swift OCR execution. This built-in multithreading simplifies development and significantly boosts performance by integrating parallelism directly into the OCR workflow. When you need to bound that parallelism - on a memory-capped container or a shared host, for example - see the section on controlling parallelism and memory usage below.

The library's multithreaded Tesseract implementation provides these key advantages:

  • Automatic CPU core utilization: IronOCR detects and uses all available cores without manual configuration, and lets you cap that with MaxDegreeOfParallelism when you need to
  • Thread-safe operations: OCR operations are thread-safe, and a single IronTesseract instance can be shared safely across concurrent reads
  • Tunable memory footprint: Each page read in parallel uses its own native Tesseract engine, so peak memory scales with the degree of parallelism - lower it to trade throughput for a smaller working set
  • Scalable performance: Processing time decreases proportionally with the number of available cores

Here is a multithreaded read example in 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

Two properties on IronTesseract let you fine-tune this behavior to match specific performance and memory requirements, covered in the next section. For a different optimization axis - trading a little accuracy for raw speed - see fast OCR configuration.

How Do I Control Parallelism and Memory Usage?

By default, IronTesseract reads as many pages concurrently as the machine has CPU cores, and each concurrent page runs its own native Tesseract engine. Two properties let you bound that when memory matters more than raw throughput:

  • MaxDegreeOfParallelism caps how many pages are OCR'd concurrently within a single Read call. Because each concurrent page costs one engine, lowering it directly lowers peak memory.
  • MultiThreaded turns parallel reading off entirely when set to false, giving the lowest and most predictable footprint. It takes precedence over MaxDegreeOfParallelism.
using IronOcr;

var ocr = new IronTesseract
{
    Language = OcrLanguage.English,
    MaxDegreeOfParallelism = 2 // OCR at most 2 pages at once per Read
};

using var input = new OcrInput();
input.LoadPdf("invoice.pdf");
var result = ocr.Read(input);
using IronOcr;

var ocr = new IronTesseract
{
    Language = OcrLanguage.English,
    MaxDegreeOfParallelism = 2 // OCR at most 2 pages at once per Read
};

using var input = new OcrInput();
input.LoadPdf("invoice.pdf");
var result = ocr.Read(input);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

WarningMultiThreaded had no effect in earlier releases - it is now a live setting. Code that set MultiThreaded = false expecting it to be inert will read sequentially after upgrading, with no compiler warning and no exception.

Please noteThese properties bound parallelism within a single Read call. Concurrency across multiple documents or multiple Read calls remains your application's responsibility - combine them with the async patterns below.

For default values, the precedence rules between the two properties, guidance on choosing a value, and details of the resolved AccessViolationException in high-volume parallel OCR, see how to limit parallel OCR memory usage.

How Do I Use Async Support in IronOCR?

Asynchronous programming optimizes OCR performance by allowing developers to execute OCR tasks without blocking the main thread. This keeps applications responsive while processing large documents or images for text recognition. Async support enables the system to handle other tasks while OCR operations run in the background. This capability is crucial when implementing OCR progress tracking in user interfaces.

This section covers async support integration in IronOCR, demonstrating different methods to make OCR services non-blocking. IronOCR's async capabilities ensure optimal performance for both desktop applications requiring responsiveness during OCR operations and web services handling multiple concurrent OCR requests.

When Should I Use OcrReadTask Objects?

OcrReadTask objects enhance control and flexibility in OCR processes with IronOCR. These objects encapsulate OCR operations, allowing efficient management of text recognition tasks. This section demonstrates using OcrReadTask objects in your IronOCR workflow and shows how they initiate and optimize OCR tasks. OcrReadTask objects help maximize IronOCR capabilities when orchestrating complex document processing or fine-tuning application responsiveness.

OcrReadTask objects work best when:

  • You need fine-grained control over task execution
  • Implementing abort token functionality for cancellable operations
  • Managing multiple concurrent OCR operations with different priorities
  • Integrating with custom task schedulers or workflow engines
: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

How Do I Use the ReadAsync Method?

ReadAsync() provides a direct mechanism for initiating OCR operations asynchronously. Without complex threading or task management, developers can integrate asynchronous OCR into their applications. This method prevents the main thread from blocking during OCR tasks, ensuring applications remain responsive.

The ReadAsync method works well for:

  • Desktop applications that need to maintain UI responsiveness
  • Web applications handling multiple simultaneous OCR requests
  • Batch processing scenarios where progress tracking is essential
  • Integration with modern async/await patterns in .NET applications
: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

Why Should I Use Async and Multithreading with IronOCR?

Combining async support and multithreading in IronOCR provides numerous benefits for modern application development:

Performance Benefits:

  • Improved Throughput: Process multiple documents simultaneously without blocking
  • Better Resource Utilization: Maximize CPU usage across all available cores
  • Reduced Latency: Start processing immediately without waiting for previous operations to complete
  • Scalable Architecture: Handle increasing workloads without architectural changes

Development Benefits:

  • Simplified Code: No need to manage threads manually - IronOCR handles the complexity
  • Modern Patterns: Full support for async/await patterns in Tesseract 5 for .NET
  • Easy Integration: Works seamlessly with existing .NET async infrastructure
  • Maintainable Solutions: Clear, readable code that follows .NET best practices

Multithreading in IronOCR optimizes OCR tasks significantly. The built-in multithreading capabilities, combined with methods like ReadAsync(), simplify handling large volumes of text data. This combination ensures applications remain responsive and efficient, making IronOCR an effective tool for creating high-performance software with streamlined text recognition capabilities. To get started with the complete feature set, check our NuGet package installation guide.

Frequently Asked Questions

How can I perform OCR asynchronously in C# without blocking my main thread?

IronOCR provides the ReadAsync method that enables non-blocking OCR execution. You can simply use `var result = await new IronOcr.IronTesseract().ReadAsync("image.png");` to perform OCR asynchronously, keeping your application responsive while processing documents.

Does OCR automatically use multiple CPU cores for better performance?

Yes, IronOCR automatically leverages all available CPU cores through built-in multithreading. The IronTesseract implementation detects and utilizes all cores without requiring manual configuration, providing optimized resource management and scalable performance.

What are the main benefits of multithreading in OCR processing?

IronOCR's multithreaded implementation offers automatic CPU core utilization, thread-safe operations, a tunable memory footprint through the MaxDegreeOfParallelism property, and scalable performance where processing time decreases proportionally with available cores.

Can I process multiple PDF documents simultaneously with async OCR?

Yes, IronOCR's ReadAsync method works particularly well when processing PDF documents or handling multiple image files simultaneously, allowing you to process multiple documents concurrently without blocking your application.

Do I need to write special code to enable multithreading for OCR?

No, multithreading is enabled by default and requires no specialized API to get started. The library seamlessly integrates parallelism directly into the OCR workflow, automatically managing threads across multiple cores for optimal performance. If you need to bound that parallelism, set IronTesseract.MaxDegreeOfParallelism or turn it off with MultiThreaded.

What is the OcrReadTask Object and how does it help with asynchronous processing?

The OcrReadTask Object in IronOCR enables you to take advantage of asynchronous concurrency when processing documents. It allows you to manage OCR operations asynchronously while the library handles the underlying multithreading complexity.

How do I limit memory usage when OCRing large batches in parallel?

Set IronTesseract.MaxDegreeOfParallelism to cap how many pages are OCR'd concurrently within a single Read call, or set MultiThreaded to false to read sequentially. Each concurrent page uses its own native Tesseract engine, so lowering the value directly lowers peak memory. See the guide on limiting parallel OCR memory usage for default values and precedence rules.

Does setting MultiThreaded to false actually change anything?

Yes. MultiThreaded had no effect in earlier releases but is now a live setting. Setting it to false reads pages sequentially on a single thread, giving the lowest and most predictable memory footprint, and it overrides MaxDegreeOfParallelism. Code that set this property expecting it to be inert will read sequentially after upgrading.

Is IronOCR fully compatible with .NET 10?

.NET 10 is supported by IronOCR via its latest release version 2025.12. You can install the library using NuGet (Install-Package IronOcr) and run async methods like ReadAsync() under .NET 10 without special configuration.

Is there a free trial available for IronOCR?

Yes, Iron Software offers a free trial of IronOCR, allowing users to test its features and capabilities before making a purchase decision.

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of ...
Read More
Ready to Get Started?
Nuget Downloads 6,224,182 | Version: 2026.8 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast? PM > Install-Package IronOcr
run a sample watch your image become searchable text.