Free Support Forum - aspose.com

BarcodeReader clearly not thread safe

I am creating an instance of the BarcodeReader class on 50 different threads, each thread receiving a different System.Drawing.Bitmap. Essentially, there are 50 instances of BarcodeReader, with 50 different images, all calling Read() at the same time.


Sometimes a barcode is detected in an image which SIMPLY DOES NOT HAVE A BAR CODE.

Sometimes odd exceptions come from BarcodeReader.Read(). ArgumentOutOfRangeExceptions and stuff.

Here is the function which is being invoked on 50 threads, with 50 different inputs, at the same time:

        IEnumerable ReadBarcodesAspose(PageImage pageImage, RecognitionHints.Orientation orientation)
{
var read = new List();
var loop = 0;
        // try until success or maximum attempt reached
        while (loop != -1 && loop < 4)
        {
            try
            {
                using (var b = new Bitmap(pageImage.Image))
                using (var r = new BarCodeReader(b, BarCodeReadType.Code39Standard))
                {
                    r.OrientationHints = orientation;
                    while (r.Read())
                        read.Add(r.GetCodeText());
                }

                loop = -1;
            }
            catch (Exception e)
            {
                loop++;
            }
        }

        if (read.Count > 0 && pageImage.Index % 2 != 0)
            Debugger.Break();

        return read;
    }</string></string></pre></div><div><br></div><div>Currently, only PageItem instances with an even Index value actually have a barcode, so I've got it breaking the debugger when it finds a barcode on a non-even page. While the debugger is broken on this line, I am running this in the immediate window:<br><br>pageImage.Image.Save(@"C:\Users\jhaltom\foo.png", ImageFormat.Png");<br><br>When I open the resulting Png, as expected, there is no barcode. However, it's read a bar code (read.Count > 0). In fact, the barcode that is returned IS on another page. A completely different page. From a completely different Image instance, which was handed to a completely different thread.</div><div><br></div><div>I want to repeat, I explicitly save the pageImage.Image variable while broken in this method, and it does not have any barcode on it, and yet, in this method, GetCodeText() has managed to return a barcode value.<br><br>When I set my code down to a single thread, this works fine.</div><div><br></div><div>I am running version 5.1.0 delivered by NuGet. It's in evaluation mode, and parsing only for Code39Standards. I'm evaluating your code to determine if I want to purchase it. Not being truly thread safe kind of hurts that. :)</div>

I should note, the odd exception handling I’ve included is probably not required: I was attempting to work around the random exceptions being thrown from BarcodeReader.Read(), in hopes of getting the solution working. However, I suspect those Exceptions are being thrown for the same thread-safety reason causing my main issue.

Hi Jerome,


Please accept our apologies for the delay. We are working over your query and will get back to you as soon as possible.

Hi Jerome,


Thank you for your interest in our Aspose products. First off, You can avoid evaluation limitations by getting a temporary license for 30 days and try the latest version at your end.

You can learn all about how Aspose products are licensed by checking out the Licenses FAQ here: http://www.aspose.com/corporate/purchase/faqs/default.aspx
http://www.aspose.com/docs/display/barcodenet/Licensing

The extended Code 39 is a modification of the NORMAL (standard) version that can encode the complete 128 ASCII character set. If code39standard does not read then you can use code39extended symbology to recognize a barcode. Code 39 Standard/Extended always go near each other. So it is good to always check for Standard and Extended together during recognition. Please modify code line as:

using (var r = new BarCodeReader(str, BarCodeReadType.Code39Standard | BarCodeReadType.Code39Extended))

While using latest versions of Aspose.BarCode v5.2 & v5.1, I’m unable to reproduce the issues as you mentioned. I’ve prepared a console application for you, please give it a try with input images and share results here in this thread. We’ll take a closer look and guide you accordingly.