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 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 improves efficiency, keeping the application running smoothly while delivering faster 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.
How to set max parallel threads in C#
- Download the IronBarcode C# library to set max parallel threads for reading barcode
- Load the barcodes from a specified folder
- Instantiate a new
BarcodeReaderOptionsobject - Set the
Multithreadedto true and provide an integer forMaxParallelThreads - Print and display the results with
BarcodeResults
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.
using Google.Protobuf.WellKnownTypes;
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, optionsFaster);
foreach (var result in results)
{
// Show the type and value for every barcode found
Console.WriteLine($"Value: {result.Value}, Type: {result.BarcodeType}");
}
}
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. MaxParallelThreads In our case, we are using a computer with 32 logical processors, so Environment.ProcessorCount will be set to 32.
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, optionsFaster);
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");
Output
Process Time with 4 Threads

In this example run on the 32-core machine described above, the processing time for this process was around 84 seconds. Actual timings vary by hardware and workload.
Process Time with Environment ProcessorCount

As you can see, the processing time for this operation was around 53 seconds in this example run, 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.
Multithreaded to true and increasing MaxParallelThreads will not improve the process speed and may actually decrease it.Frequently Asked Questions
How does setting maximum parallel threads optimize barcode generation performance?
Setting a maximum number of parallel threads optimizes performance by utilizing all available processor cores, allowing multiple barcode images to be processed simultaneously. This increases processing power and reduces the time required for batch jobs.
How do you set the max parallel threads using IronBarcode in C#?
To set max parallel threads in IronBarcode, instantiate a `BarcodeReaderOptions` object with `Multithreaded` set to true and assign an integer value to `MaxParallelThreads`. This setup will determine the number of threads executing barcode processing.
What is the default number of max parallel threads in IronBarcode?
The default value for `MaxParallelThreads` in IronBarcode is set to 4. This initial configuration is aimed at ensuring efficient processing on most multi-core systems.
Can the value of MaxParallelThreads exceed the number of processor cores?
While there is no hard limit, setting `MaxParallelThreads` to values greater than the available logical cores can degrade performance due to excessive context switching and overhead.
What should be considered when setting the MaxParallelThreads value?
Consider the number of logical processors in your hardware when setting `MaxParallelThreads`. Optimal values allow for efficient multi-threading without overwhelming the processor, typically using the number of cores minus one for best performance.
What is the effect of using Environment.ProcessorCount for setting MaxParallelThreads?
Using `Environment.ProcessorCount` sets `MaxParallelThreads` to the total number of logical processors, making full use of available threading resources to improve processing times considerably on multi-core machines.
What impact does hardware capacity have on multi-threaded barcode processing?
Hardware capacity directly influences the effectiveness of multi-threading. The number of logical cores determines the feasible number of threads, balancing workload and avoiding performance bottlenecks due to resource limitations.
Why might increasing MaxParallelThreads not always improve performance?
Increasing `MaxParallelThreads` can overload the CPU with context switches, especially if it surpasses the core count, leading to diminishing returns and potential slowdowns rather than performance gains.
What happens if the specified image directory does not exist in the example code?
If the specified directory does not exist, the code includes a check that outputs an error message and exits the process to prevent crashes during barcode reading.
What is the recommended approach for determining the optimal MaxParallelThreads value?
Testing different configurations in your specific environment, factoring in your system's resources and workload type, is recommended to find the optimal `MaxParallelThreads` value for maximizing performance.

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.