Barcode reader and Parallel.For issue

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

Moderator: Alex

Post Reply
gendalf
Posts: 1
Joined: Mon Nov 26, 2012 11:37 am

Barcode reader and Parallel.For issue

Post by gendalf » 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?

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

Alex
Site Admin
Posts: 1464
Joined: Thu Jul 10, 2008 2:21 pm

Re: Barcode reader and Parallel.For issue

Post by Alex » Tue Feb 26, 2013 8:32 am

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest