---
title: "Share one OCR engine across C# worker threads"
url: https://recursiveai.org/oneocr/cookbook/reuse-ocr-engine-across-workers/
description: "Share one thread-safe OCR engine across C# workers. Read independent PNGs with Parallel.ForEach, or keep input order with OneOcr’s batch API."
version: "1.0.0"
---

# Share one OCR engine across C# worker threads

**OneOcr** is a free, open-source OCR library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneOcr) · [GitHub](https://github.com/recursive-ai-oss/oneocr-csharp) · [Docs](https://recursiveai.org/oneocr/docs/) · [Download example (.zip)](https://recursiveai.org/oneocr/cookbook/reuse-ocr-engine-across-workers/reuse-ocr-engine-across-workers.zip)

[The engine is immutable and thread-safe](https://github.com/recursive-ai-oss/oneocr-csharp/blob/8db430fb77c4d86d51686f15dfd9b5ccc20c99a7/README.md#L96-L99), so both workers can share it.

```shell
dotnet add package RecursiveAI.OneOcr
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneImage;
using OneOcr;
var engine = OcrEngine.LoadBundled();
string[] files = ["north.png", "south.png"];
Parallel.ForEach(files, file =>
    Console.WriteLine($"{file}:\n" +
        Image.Load(file).ToText(engine)));
```

Unzip [the example](https://recursiveai.org/oneocr/cookbook/reuse-ocr-engine-across-workers/reuse-ocr-engine-across-workers.zip), then `dotnet run`.

The two PNGs in the zip say “North entrance” and “South entrance”. One run printed this; either job can print first:

```text
south.png:
South entrance
north.png:
North entrance
```



Each worker loads its own image and prints the filename with its text in one call. If a worker fails to load its image, the loop reports the failure through `AggregateException`; another job may already have printed its result.

## Variations

### Read a batch in input order

Replace the `Parallel.ForEach` call in the snippet at the top with:

```csharp
var images = files.Select(file => Image.Load(file));
var pages = engine.Ocr(images);
foreach (var page in pages)
    Console.WriteLine($"{page.Index}: {page.ToText()}");
```

```text
0: North entrance
1: South entrance
```

## Related recipes

[Convert an image to text](https://recursiveai.org/oneocr/cookbook/extract-text-from-image/) · [Batch OCR images in input order](https://recursiveai.org/oneocr/cookbook/batch-ocr-images-in-input-order/)

---

## The short answer

Create one `OcrEngine` with `LoadBundled()` and pass that same instance to each worker. The engine is immutable and thread-safe. Keep the input and result for each job separate: in the example, each worker owns its own `Image` and labels the text with its filename.

## How do I run OCR on multiple threads in C#?

For independent jobs, keep the engine in a long-lived field or capture it outside the worker loop as shown above. Load each image inside its job. If the work comes from a queue, label the result with the job ID so you can match it to the input regardless of which worker finishes first. Sharing the engine does not make your own mutable result collection thread-safe.

Worker count and OCR’s internal thread count are separate. `OcrOptions.MaxDegreeOfParallelism` controls the threads used inside recognition; the documented default is the smaller of four and the core count. If several large images are in flight, bound the number of jobs you start instead of loading the whole queue into memory.

## How do I keep OCR results in input order?

Use the batch overload when you have a fixed set of images and want a list in input order. The variation supplies `north.png` first, so the north sign is at index 0 even if an independent worker loop happens to finish the south sign first. `OcrAsync(images)` preserves that same input order. When awaiting an async call, keep its input images alive until it completes.

Batching is an ordering choice, not an engine lifetime requirement. You can reuse one engine across separate calls, across batches, or both.

Source: [OneOcr quick start and threading notes at v1.0.0](https://github.com/recursive-ai-oss/oneocr-csharp/blob/8db430fb77c4d86d51686f15dfd9b5ccc20c99a7/README.md#L83-L105) and [batch behavior](https://github.com/recursive-ai-oss/oneocr-csharp/blob/8db430fb77c4d86d51686f15dfd9b5ccc20c99a7/README.md#L130-L194).
