How to Use Async and Multithread in C# with IronBarcode
Async and multithreading in IronBarcode optimize barcode reading performance differently - async prevents blocking the main thread during I/O operations while multithreading processes multiple barcodes simultaneously across CPU cores.
Developers often confuse Async and Multithreading operations. Both methods enhance program performance and efficiency by optimizing system resource utilization and reducing runtime. However, they differ in approach, mechanisms, and use cases. IronBarcode supports both approaches. This article explores their differences and implementation using IronBarcode.
Quickstart: Async & Multithreaded Barcode Reading Example
Use this one-line example to get started instantly with IronBarcode. It shows how easy it is to combine asynchronous reading and multithreading options to scan multiple barcode images in parallel with minimal setup.
Get started making PDFs with NuGet now:
Install IronBarcode with NuGet Package Manager
Copy and run this code snippet.
var results = await IronBarCode.BarcodeReader.ReadAsync(imagePaths, new IronBarCode.BarcodeReaderOptions { Multithreaded = true, MaxParallelThreads = 4, ExpectMultipleBarcodes = true });Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# library for async and multithread support
- Use
ReadAsyncandReadPdfAsyncmethods for asynchronous barcode reading from images and PDFs - Enable multithreading with the Multithreaded property set to 'true'
- Specify parallel thread count using the MaxParallelThreads property
- Check the performance comparison between normal, async, and multithreaded barcode reading
How Do I Read Barcodes Asynchronously with IronBarcode?
Asynchronous reading enables long or blocking operations to proceed without blocking the main thread's execution. In C#, use the async and await keywords with methods supporting asynchronous features. This approach doesn't create additional threads but releases the current thread. While the main thread initiates and manages tasks, it doesn't remain exclusively devoted to a single task. The main thread returns when the asynchronous method requires its involvement, freeing it to handle other tasks when not needed—particularly useful for I/O-bound tasks like reading/writing files or making network requests.
Consider barcode reading as an example. The process involves:
- Reading the file
- Applying reading options
- Decoding the barcode
During file reading, the main task can be released. This benefits scenarios with multiple image files or large PDFs, as demonstrated in our reading barcodes tutorial.
Use ReadAsync and ReadPdfAsync methods to read barcodes asynchronously for images and PDF documents, respectively. Before implementing async operations, ensure you've installed IronBarcode via NuGet in your project.
:path=/static-assets/barcode/content-code-examples/how-to/async-multithread-async.csusing IronBarCode;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
List<string> imagePaths = new List<string>() { "image1.png", "image2.png" };
// Barcode reading options
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true
};
// Read barcode using Async
BarcodeResults asyncResult = await BarcodeReader.ReadAsync(imagePaths, options);
// Print the results to console
foreach (var result in asyncResult)
{
Console.WriteLine(result.ToString());
}The code snippet above instantiates a List of image paths to be read asynchronously by IronBarcode. To read the images, use the ReadAsync method from the BarcodeReader class. Specify the imagePaths and reading options. For advanced configuration options, refer to our guide on barcode reader settings.
This asynchronous operation method is also available for reading barcodes in PDF documents through ReadPdfAsync in the same class. For specific PDF reading configurations, see our PDF barcode reader settings guide.
When Should I Use Async Reading Over Regular Methods?
Asynchronous reading excels in several scenarios:
GUI Applications: Windows Forms or WPF applications requiring UI responsiveness. Async prevents interface freezing during barcode scanning.
Web Applications: ASP.NET applications handling multiple concurrent requests without blocking threads, especially when processing uploaded barcode images.
Batch Processing: Sequential reading of multiple barcode images or PDFs, allowing other tasks to execute during I/O operations.
- Network Operations: Reading barcodes from remote sources or URLs, as shown in our read barcodes from URL asynchronously example.
Why Does Async Reading Improve Application Responsiveness?
Asynchronous reading improves responsiveness by freeing the main thread during I/O-bound operations. When IronBarcode reads an image file from disk or processes a PDF, the thread doesn't wait idle. Instead, it handles other tasks like responding to user input or processing requests. This is especially noticeable when dealing with:
- Large image files requiring significant load time
- PDFs with multiple pages containing barcodes
- Network-based image sources
- Scenarios requiring image correction filters before barcode detection
What Are Common Pitfalls When Using Async Barcode Reading?
When implementing async barcode reading, watch for these common issues:
Deadlocks: Avoid
ResultorWait()on async methods in UI contexts. Always useawaitthroughout the call chain.Exception Handling: Wrap async calls in try-catch blocks as exceptions in async methods may not propagate as expected.
Context Switching: Consider
ConfigureAwait(false)usage when you don't need to return to the original synchronization context.- Performance Misconceptions: Async doesn't accelerate individual operations; it improves application responsiveness. For speed improvements with multiple images, consider multithreading.
For troubleshooting async-related issues, consult our barcode recognition troubleshooting guide.
How Do I Enable Multithreaded Barcode Reading?
Unlike asynchronous operations, multithreading executes a single process across multiple threads simultaneously. Instead of sequential execution in a single thread, multithreading divides tasks among multiple threads for concurrent execution. Multithreading requires multiple CPU cores, as these cores independently execute threads. Like asynchronous operations, multithreading enhances application performance and responsiveness.
In IronBarcode, enable multithreading by setting the Multithreaded property and specifying maximum cores for concurrent execution using MaxParallelThreads in BarcodeReaderOptions. The default MaxParallelThreads value is 4, adjustable based on available CPU cores. For optimal performance configurations, see our reading speed options guide.
:path=/static-assets/barcode/content-code-examples/how-to/async-multithread-multithread.csusing IronBarCode;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
List<string> imagePaths = new List<string>(){"test1.jpg", "test2.png"};
// Barcode reading options
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
Multithreaded = true,
MaxParallelThreads = 4,
ExpectMultipleBarcodes = true
};
// Read barcode with multithreaded enabled
BarcodeResults results = BarcodeReader.Read(imagePaths, options);
// Print the results to console
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}How Much Performance Improvement Can I Expect?
Let's read two sample images and compare reading times across normal, asynchronous, and multithreaded operations.
Sample Image


| Normal Read | Asynchronous Read | Multithreaded Read (4 cores) |
|---|---|---|
| 01.75 seconds | 01.67 seconds | 01.17 seconds |
The comparison shows significant performance increases with asynchronous and multithreaded reading. Multithreading provides approximately 33% improvement over normal reading, while async delivers about 5% improvement. However, these operations serve different purposes and approaches. Choose the approach that best suits your application requirements.
Performance improvements vary based on:
- Number of images processed
- Image complexity and barcode quality
- Available CPU cores
- Other system resources
For situations with multiple barcodes on a single document, visit the Read Multiple Barcodes guide.
When Should I Choose Multithreading Over Async Operations?
Choose multithreading when:
- CPU-Bound Operations: Processing involves heavy computation like complex image filters or high-resolution images
- Batch Processing: Multiple independent images require simultaneous processing
- Multi-Core Systems: Deployment environment has multiple CPU cores available
- Performance Critical: Raw processing speed outweighs resource efficiency
Choose async operations when:
- I/O-Bound Operations: Most time involves reading files or waiting for network responses
- UI Applications: Maintaining responsive user interfaces is crucial
- Limited Resources: Running on systems with limited CPU cores
- Web Applications: Handling multiple concurrent requests efficiently
How Do I Determine the Optimal MaxParallelThreads Value?
The optimal MaxParallelThreads value depends on several factors:
- Available CPU Cores: Start with
Environment.ProcessorCountas baseline - Workload Type: For pure barcode reading, use 75% of available cores
- System Resources: Leave headroom for OS and other processes
- Testing Results: Benchmark with your specific workload
Here's a practical approach to finding the optimal value:
int optimalThreads = Math.Max(1, Environment.ProcessorCount - 1);int optimalThreads = Math.Max(1, Environment.ProcessorCount - 1);For production environments, monitor performance and adjust based on actual usage patterns. Consider implementing license key configuration for enterprise deployments requiring maximum performance.
For complete API capabilities, consult the IronBarcode API Reference.
Frequently Asked Questions
What is the difference between async and multithreading when reading barcodes?
With IronBarcode, async operations prevent blocking the main thread during I/O tasks like file reading, while multithreading processes multiple barcodes simultaneously across CPU cores. Async uses the async/await keywords to release the main thread during long operations, whereas multithreading creates multiple threads to handle barcode processing in parallel.
How do I implement asynchronous barcode reading in C#?
IronBarcode provides ReadAsync() and ReadPdfAsync() methods for asynchronous barcode reading. Simply use these methods with the async/await keywords in C# to read barcodes from images and PDFs without blocking the main thread. This is particularly beneficial when processing multiple image files or large PDF documents.
How can I enable multithreading for barcode processing?
To enable multithreading in IronBarcode, set the Multithreaded property to 'true' in the BarcodeReaderOptions. You can also control the number of parallel threads using the MaxParallelThreads property, allowing you to optimize performance based on your system's capabilities.
Can I combine async and multithreading for optimal performance?
Yes, IronBarcode allows you to combine both approaches. You can use ReadAsync() with multithreading enabled by setting Multithreaded = true and configuring MaxParallelThreads in the BarcodeReaderOptions. This combination provides both non-blocking I/O operations and parallel processing across multiple CPU cores.
When should I use async vs multithreading for barcode reading?
Use async operations in IronBarcode when dealing with I/O-bound tasks like reading large files or making network requests, as it prevents UI freezing. Use multithreading when you need to process multiple barcodes simultaneously and have CPU resources available. For best results, combine both approaches when processing multiple large barcode images or PDFs.






