How to scan, read barcodes in multithreading
and async in C#?


Read barcodes in multithreading in C# ASP.NET, MVC web and Windows application?

How to tutorials to instruct scan, read barcode images in Async and multithreading in C# ASP.NET, MVC, web and Windows applications



This C# how-to page will instruct you how to use and apply multithreading and async programming using C# barcode reader library.

  • Enhance barcode reading performace by executing multiple threads on large number of barcode image files
  • Scanning and reading more than 20 barcode types with any rotation angles
  • Support QR Code and barcode reading using C# on .NET 10, 9, 8, 7, 6, .NET Standard, .NET Core, .NET Framework 4.x, 3.x, 2.x

How to read barcodes from image using C# multi-threading?

  1. Download C# Barcode Reader Library
  2. Install C# library to read barcode from images in C# program
  3. Step by Step Tutorial








Multithreading in C# Barcode Scanner

Top

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.
  • 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.




How to read barcodes in multi-threading using C#?

Top
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#.
  • 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#?

Top
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.
  • 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 ... 
}












Common Asked Questions

What is multithreading barcode reading in C#?

Multithreading is a core feature in operating systems that support a program (process) to do several jobs (threads) independently. Using multithread barcode scanning will signifiantly improve the barcode reading speed in multiple CPU core processors computer.

How to enable multi-thread barcode reading in C#?

You can easily configure and enable multithreading barcode scanning through scan option ScanOptions object. Set method SetMaxMultiThreadCount() with the maximum number of threads in barcode scanning. The number of barcode scanning threads should no more than the number of CPU core processors in the running workstation or web server.

What is asynchronous barcode scanning in C#?

Asynchronous programming in C# allows you to scan barcodes which performs tasks without blocking the main thread. It will improve the barcode scanning performance also.

How to enable asynchronous barcode scanning in C#?

Using OnBarcode C# Barcode Reader library, you can call method BarcodeScanner.ScanAsync() to scan barcodes asynchronously in C# ASP.NET, MVC web and WinForms, WPF desktop applications.

What is the difference between multi-thread and async barcode reading in C#?

Multi-threading will help concurrent execution of multiple barcode scanning tasks by using mutliple threads. And Async processing will release the current barcode reading thread (or main thread) during idle time. Async processing is based on the task scheduling module, and uses the Async/Await keywords to release the main barcode reading thread. For example, await BarcodeScanner.ScanAsync() to apply async processing on barcode reading in C# application.

Can I enable both multithreading and asynchronous barcode reading in C#?

Yes. OnBarcode C# Barcode Reader library supports enable both multithreading and asynchronous barcode reading in C# program. You can configure and apply multithreading barcode reading in method SetMaxMultiThreadCount() in barcode scanner option, and call method BarcodeScanner.ScanAsync() to enable asynchronous barcode reading in C# application. The combination will help barcode scanning performance on multiple CPU core processors.




































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.