Question

Hello fellow programmers!

I`m developing a Windows Forms .NET Compact Framework 2.0 for a Windows Mobile 6.1 device that has a barcodereader hardware.

I can use the barcodereader to read barcodes, and I can activate and deactivate it as well. Except that when I try to read something and go to the next form I get a objectdisposedexception. That happens (I guess) because I have to dispose the instance of the barcode reader and then create a new one in the next form.

The problem is: when I use a button to go to the next form, using the same code to dispose the barcodereader I don`t have the objectdisposedexception. When I simply put the form load on the textchanged event the error rises, but is not caught by any try/catch statements, making the application crash.

I can`t debug it either, because the VS emulator to windows mobile does not work with the device barcodereader DLL.

Can someone help me?

Here`s the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

//DLL that controls the barcodereader
using Intermec.DataCollection;

namespace WOPT_Coletor.view.ConsultarPosicao
{
    public partial class frmConsultarPosicao_2 : Form
    {

        public BarcodeReader leitor;

        public frmConsultarPosicao_2()
        {
            InitializeComponent();
            ShowHide.ShowTopStatusbar(false);

            //code to work with the barcode reader
            model.LeitorCodigoDeBarras classeLeitor = new model.LeitorCodigoDeBarras();
            leitor = classeLeitor.LerCodigoDeBarras();
            leitor.BarcodeRead += new BarcodeReadEventHandler(this.eventoLeitorCodigoDeBarrasArmazenagem1);
        }    

        //Event to receive the barcode reading information
        void eventoLeitorCodigoDeBarrasArmazenagem1(object sender, BarcodeReadEventArgs e)
        {
            tbCodMaterial.Text = e.strDataBuffer.Trim();
        }

        private void tbCodMaterial_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (tbCodMaterial.Text.Length == 23)
                {                        
                    Cursor.Current = Cursors.WaitCursor;
                    Cursor.Show();

                    //disposal of the barcodereader instance
                    leitor.ScannerOn = false;
                    leitor.ScannerEnable = false;
                    leitor.Dispose();
                    leitor = ((BarcodeReader)null);

                    //processing of the information read.
                    char[] auxcodMaterial = new char[9];

                    using (StringReader str = new StringReader(tbCodMaterial.Text))
                    {
                        str.Read(auxcodMaterial, 0, 8);
                    }    
                    string codMaterial = new string(auxcodMaterial);

                    //loads next form
                    Form destino = new frmConsultarPosicao_3(codMaterial);
                    destino.Show();

                    Cursor.Current = Cursors.Default;
                    Cursor.Show();

                    //closes and dispose of the current form
                    this.Close();
                    this.Dispose(true);
                }    
            }

            catch (ObjectDisposedException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
}
Was it helpful?

Solution

Without knowing more about your barcode reader's API and behavior, I'd guess it's that you have a race condition where your BarCodeRead event could fire while you're inside tbCodMaterial_TextChanged. I would suggest placing a synchronization block around the code that disables the scanner, and inside the block only perform the shutdown if the scanner is non-null:

private readonly Object mySynchronizationObject = new Object;
...
lock (mySynchronizationObject)
{
    if (leitor != null)
    {
         //disposal of the barcodereader instance
         ...
    }
}

It also wouldn't hurt to disconnect from the event prior to shutdown (inside the above lock):

leitor.BarcodeRead -= new BarcodeReadEventHandler(this.eventoLeitorCodigoDeBarrasArmazenagem1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top