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
















OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.