Barcode reader and Parallel.For issue
Posted: Mon Feb 25, 2013 8:18 pm
Hello! I try to use your BarcodeReader v7.0, compiled against dotNet 4. I want run parallel recognition of images using Parallel.For, but if I use a single instance of Vintasoft BarcodeReader, that shared between all of my workers, there is NullReferenceException raising from BarcodeReader instance, when BarcodeReader.ReadBarcodes(string) called.
It is not looks like bad synchronization in my code. Are Vintasoft BarcodeReader isntances thread safe? If no, which best practices of instantiation you can recommend to use? How much is costs to create and dispose BarcodeReader instance in each worker routine? Did this class uses unmanaged code and structures?
It is not looks like bad synchronization in my code. Are Vintasoft BarcodeReader isntances thread safe? If no, which best practices of instantiation you can recommend to use? How much is costs to create and dispose BarcodeReader instance in each worker routine? Did this class uses unmanaged code and structures?
Code: Select all
void Recognize()
{
BarcodeReader reader = new BarcodeReader();
_setupReader(reader);
var files =
Directory.EnumerateFiles(_baseDirectory, "*.jpg");
Parallel.ForEach(
files,
(file) =>
{
var result = reader.ReadBarcodes(file);
lock (recognitionPages) // recognitionPages is just a System.Collections.Generic.Dictionary<string, IBarcodeInfo[]>
{
recognizedPages[file] = result;
}
}
);
}
static void _setupReader(BarcodeReader reader)
{
reader.Settings.ScanDirection =
ScanDirection.Vertical |
ScanDirection.Horizontal;
reader.Settings.ScanBarcodeTypes =
BarcodeType.Code39 |
BarcodeType.Code128;
reader.Settings.ExpectedBarcodes = 2;
}