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
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.
:path=/static-assets/barcode/content-code-examples/how-to/set-max-parallel-thread.csusing 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}");
}
}
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 forMaxParallelThreadsdepends 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.csusing 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");Output
Process Time with 4 Threads

The processing time for this process is 84 seconds.
Process Time with Environment ProcessorCount

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.
Multithreaded to true and increasing MaxParallelThreads will not improve the process speed and may actually decrease it.Frequently Asked Questions
What is the purpose of setting maximum parallel threads in IronBarcode?
Setting the maximum parallel threads allows you to optimize the performance of barcode generation by efficiently using system resources, especially when processing a large number of barcodes in bulk.
How can I configure the maximum parallel threads in IronBarcode?
You can configure the maximum parallel threads in IronBarcode by using the appropriate method in your C# code to set the desired number of threads for barcode generation tasks.
Why is it important to optimize performance for bulk barcode creation?
Optimizing performance for bulk barcode creation ensures that the process is efficient and fast, reducing the time and resources required to generate a large volume of barcodes, which is essential for applications with high throughput demands.
What are the benefits of using parallel processing in IronBarcode?
Parallel processing in IronBarcode allows for faster barcode generation by utilizing multiple CPU cores, which leads to improved application performance and reduced processing time for large-scale barcode tasks.
Can setting too many parallel threads affect performance negatively?
Yes, setting too many parallel threads can lead to resource contention and increased overhead, potentially reducing performance. It is important to find a balanced configuration that matches your system's capabilities.
What considerations should be made when choosing the number of parallel threads?
Considerations include the number of available CPU cores, the system's workload, and the nature of your barcode generation tasks. It's best to experiment with different settings to find the optimal configuration.
Is there a default setting for parallel threads in IronBarcode?
IronBarcode may have a default setting for parallel threads, but it is recommended to customize this setting based on your specific application needs to achieve the best performance.
How does IronBarcode handle thread management?
IronBarcode leverages .NET's threading capabilities to manage parallel processing, allowing developers to specify the number of threads to optimize performance effectively.
Can I change the number of parallel threads dynamically during execution?
Changing the number of parallel threads dynamically during execution may not be recommended as it can lead to inconsistencies and affect performance stability. It's best to set the configuration before starting the barcode generation process.
What are some common use cases for setting maximum parallel threads in barcode generation?
Common use cases include applications that require high-speed barcode generation, such as inventory management systems, retail point-of-sale systems, and logistics applications where processing large volumes of barcodes quickly is critical.






