VintaSoft Barcode .NET SDK Discussions

Questions, comments and suggestions concerning VintaSoft Barcode .NET SDK.

Board index < VintaSoft Barcode < VintaSoft Barcode .NET SDK Discussions

We are migrating to new forums engine, no new registration or posting currently available. TIA for your patience.

Barcode reader and Parallel.For issue



Barcode reader and Parallel.For issue

Post by gendalf »

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?
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;
}


Re: Barcode reader and Parallel.For issue

Post by Alex »

Hello,

Any instance of the BarcodeReader class is not thread safe and you need use different instances of the BarcodeReader class in different threads.

Best regards, Alexander


Page 1 from 1: 1