How to Set Maximum Parallel Threads

When reading large volumes of barcodes, relying on a single-threaded process can create performance bottlenecks and limit scalability. However, using parallel threads allows your application to process multiple images simultaneously, effectively multiplying total processing power and drastically reducing the time it takes to finish a batch job.

Setting a maximum limit on these threads is a powerful way to optimize performance. It ensures the application utilizes the hardware's full potential by balancing the workload across processor cores. This approach maximizes efficiency, keeping the application running smoothly while delivering the fastest possible results.

IronBarcode provides a simple way to control this limit, ensuring optimal machine performance is achieved. The following section demonstrates how to easily set these thread limits.

Get started with IronBarcode



Set Max Parallel Threads

For this example, we will use a large set of barcode images to illustrate the scalability and efficiency of using a multi-threaded process instead of a single-threaded one. You can download the image folder here.

To configure IronBarcode to use more than one thread, a new BarcodeReaderOptions object is first instantiated with Multithreaded set to true. Afterward, the MaxParallelThreads property is set by assigning an integer value. By default, MaxParallelThreads is set to 4.

After configuring the settings, a large number of barcode images are imported from the folder. Then, using a loop, the barcode image directory is read using the Read method, passing the file path and the configured BarcodeReaderOptions. Finally, the barcode value and type are displayed by accessing the BarcodeResults.

:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread.cs
using IronBarCode;
using System;
using System.IO;

int maxParallelThreads = 4;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to 4
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
};

// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Retrieve all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file);

    foreach (var result in results)
    {
        // Show the type and value for every barcode found
        Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");
            
    }
    
}

$vbLabelText   $csharpLabel

Output

Multithreaded output

As shown in the console output, it displays the barcode value and type for each corresponding image.

Setting the Appropriate Max Parallel Thread

When the Multithreaded property is set to true, the MaxParallelThreads property defaults to 4. Although there is no hard limit for the integer assigned to MaxParallelThreads, setting the value higher than your hardware's logical core capacity can actually result in a decrease in performance. This is because the processor cannot handle excessive context switching, potentially resulting in overhead rather than speed. As such, the correct value for MaxParallelThreads depends on the computer's specifications, and developers should test to find the optimal value for their environment.

In this example, we will showcase the same multi-threaded scenario from above, but with a timer in place to compare the default value of 4 against using Environment.ProcessorCount to utilize all available threads. In our case, we are using a computer with 32 logical processors, so MaxParallelThreads will be set to 32.

:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread-performance.cs
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

// Set the max parallel threads to the number of processor cores
int maxParallelThreads = Environment.ProcessorCount;


var optionsFaster = new BarcodeReaderOptions
{
    // Set Max threads to the number of processor cores
    Multithreaded = true,
    MaxParallelThreads = maxParallelThreads,
    ExpectMultipleBarcodes = true,
};

// Start timing the process
var stopwatch = Stopwatch.StartNew();
// Dynamically get the "images" folder in the current directory
string folderPath = Path.Combine(Directory.GetCurrentDirectory(), "images");

// Check if directory exists to prevent crashes
if (!Directory.Exists(folderPath))
{
    Console.WriteLine($"Error: The directory '{folderPath}' does not exist.");
    return;
}

// Get all JPG files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");
            
        }
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($" Max parallel threads of {maxParallelThreads} with {stopwatch.Elapsed.TotalSeconds:F2}s");
$vbLabelText   $csharpLabel

Output

Process Time with 4 Threads

4 Processor

The processing time for this process is 84 seconds.

Process Time with Environment ProcessorCount

32 Processor

As you can see, the processing time for this operation is 53 seconds, which is significantly faster than running it with only four threads. However, please note that using more threads does not guarantee improved performance, as it depends on the host processor. A general rule of thumb is to use the maximum number of processors available minus one, ensuring there is still a single thread available for other system operations.

Warning The project environment must be configured to allow multi-threading. Otherwise, setting Multithreaded to true and increasing MaxParallelThreads will not improve the process speed and may actually decrease it.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 2,015,591 | Version: 2025.12 just released