Multithreading in C# Barcode Scanner
What is multithreading in C#?
Multithreading is a core feature in operating systems that support a program (process) to do several jobs (threads) independently.
Behind the scenes, the operating system rapidly switches between multiple threads, it feels like all threads are running in parallel.
Multihreading in C# is a superpower for writing responsive, efficient, and modern web and desktop applications.
- A process is an instance of a running program. It has its own memory and resources. A process can create other processes which are known as child processes.
- A thread is a smaller unit of work within a process. It is often called "lightweight process". Threads share some features of processes but are smaller and faster. One thread is always part of one specific process. Multiple threads owned by the same process can run instructions independently but share the same memory.
Multihreading in C# is a superpower for writing responsive, efficient, and modern web and desktop applications.
Why multithreading in barcode scanning and reading?
Do barcode scanning and reading in multithreading using C#, it will enhance the barcode scanner performance.
Multiple barcode reading tasks are executing concurrently in one C# program. You can view the direct result from our demo program
below. View the barcode reader performance result after running the multithread on sample barcode images.
Note that you cannot run the multithread barcode reading on all workstations and servers. Multithreading requires a server or workstation with multi-core processors, which allow executing multiple threads concurrently.
Note that you cannot run the multithread barcode reading on all workstations and servers. Multithreading requires a server or workstation with multi-core processors, which allow executing multiple threads concurrently.
How to read barcodes in multi-threading using C#?
Using C# multi-thread barcode scanning, you can run and read barcodes in several threads at the same time in one single application.
By leveraging multi-threading barcode reading, barcode library can perform multiple barcode scanning and processing tasks simultaneously, making them faster and more efficient.
Now most of modern CPUs have multiple CPU cores, using multi-threading on barcode scanning and reading will improve the reading performance and reliability.
Using OnBarcode C# Barcode Reader library, you can easily enable multithreading by calling the method SetMaxMultiThreadCount() with specifying the maximum threads for reading barcodes simultaneously in ScanOptions.
The C# source code below explains how to scan and read barcode images using multithreading in C#.
Here is a sample result after running the sample images.
Using OnBarcode C# Barcode Reader library, you can easily enable multithreading by calling the method SetMaxMultiThreadCount() with specifying the maximum threads for reading barcodes simultaneously in ScanOptions.
The C# source code below explains how to scan and read barcode images using multithreading in C#.
- Create a ScanOptions object with target scanning barcode formats specified. Here the barcode library will scan all QR Code and Data Matrix from the image list.
- Specify the maximum threads to scan barcodes using method SetMaxMultiThreadCount(int threadCount) in ScanOptions object.
If threadCount is 0, the multithreading will be disabled in the barcode reading program.
To enable multi-threading barcode reading, threadCount should be an integer and be greater than 0. - Note: The barcode library does not limit the number of thread. More threads may reduce the barcode reading speed. We recommend you to apply multi-thread in 2-4 threads.
String folder = @"C:\Output\"; String[] sourceImageFiles = new String[] { folder + "SourceImage01.png", folder + "SourceImage02.png", folder + "SourceImage03.png", folder + "SourceImage04.png", folder + "SourceImage05.png", folder + "SourceImage06.png", folder + "SourceImage07.png", folder + "SourceImage08.png" }; // Initial a ScanOptions object for scanning DataMatrix and QRCode. List<BarcodeType> types = new List<BarcodeType>() { BarcodeType.DataMatrix, BarcodeType.QRCode }; ScanOptions options = new ScanOptions(types); // Set maximum number of threads could be used in the scanning. // The valud must be non-negative. // 0: disable multi-thread (Default Value) // >0: using multi-thread options.SetMaxMultiThreadCount(3); BarcodeDetail[] result = BarcodeScanner.Scan(sourceImageFiles, options); foreach (BarcodeDetail obj in result) { // Report result ... }
Here is a sample result after running the sample images.
| Threads Count | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| Time Cost (s) | 16.1 | 15.607 | 7.043 | 5.559 | 4.099 | 4.359 | 4.736 | 4.913 | 3.448 | 3.459 |
How to read barcodes asynchronously using C#?
Asynchronous programming in C# allows you to scan barcodes
which performs tasks without blocking the main thread, making your applications more responsive and efficient.
Barcode Reader library usually perform the following steps to scan, read barcodes from image file.
Use the method ScanAsync() to read barcodes asynchronously for reading barcode files and decoding barcodes in C# program.
Barcode Reader library usually perform the following steps to scan, read barcodes from image file.
- Read an image file
- Pre-process the image before barcode reading, as configured in the ScanOptions
- Scan and decode barcode
Use the method ScanAsync() to read barcodes asynchronously for reading barcode files and decoding barcodes in C# program.
String folder = @"C:\Output\"; String[] sourceImageFiles = new String[] { folder + "SourceImage01.png", folder + "SourceImage02.png", folder + "SourceImage03.png", folder + "SourceImage04.png" }; // Initial a ScanOptions object for scanning DataMatrix. ScanOptions options = new ScanOptions(BarcodeType.DataMatrix); BarcodeDetail[] result = await BarcodeScanner.ScanAsync(sourceImageFiles, options); foreach (BarcodeDetail obj in result) { // Report result ... }
