【问题标题】:objectdisposedexception c#c#中的objectdisposedexception
【发布时间】:2013-03-01 01:56:11
【问题描述】:

各位程序员大家好!

我正在为具有条形码阅读器硬件的 Windows Mobile 6.1 设备开发 Windows Forms .NET Compact Framework 2.0。

我可以使用条形码阅读器读取条形码,也可以激活和停用它。 除了当我尝试阅读某些内容并转到下一个表单时,我得到一个 objectdisposedexception。发生这种情况(我猜)是因为我必须处理条形码阅读器的实例,然后在下一个表单中创建一个新实例。

问题是:当我使用按钮转到下一个表单时,使用相同的代码处理条形码阅读器我没有 objectdisposedexception。当我简单地将表单加载到 textchanged 事件时,错误会上升,但不会被任何 try/catch 语句捕获,从而导致应用程序崩溃。

我也无法调试它,因为 Windows Mobile 的 VS 模拟器无法与设备条形码阅读器 DLL 一起使用。

有人可以帮我吗?

代码如下:

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

【问题讨论】:

  • 是否有理由同时调用 Close 和 Dispose? Close 方法已经调用了 Dispose。
  • 这是Intermec 软件吗?查看“BarcodeReadEventHandler”的热门谷歌点击,很多人都有你的问题。当然,请先联系供应商寻求帮助,他们会知道这个问题。
  • 迈克,其实我没有理由。只是把它测试(绝望?大声笑)
  • 是的,它是 Intermec 软件。我ve read all posts Ive 在网上找到了,没有成功!但是tks!

标签: c# winforms objectdisposedexception windows-mobile-6.1


【解决方案1】:

在不了解您的条形码阅读器的 API 和行为的更多信息的情况下,我猜您有一个竞争条件,当您在 tbCodMaterial_TextChanged 中时,您的 BarCodeRead 事件可能会触发。我建议在禁用扫描器的代码周围放置一个同步块,并且在该块内仅在扫描器非空时执行关闭:

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

在关闭之前断开与事件的连接也没有什么坏处(在上述锁内):

leitor.BarcodeRead -= new BarcodeReadEventHandler(this.eventoLeitorCodigoDeBarrasArmazenagem1);

【讨论】:

  • 感谢您的回复,但它仍然不起作用...有什么建议吗?
  • 嗯。好的,是时候进行一些诊断了。当您确实看到异常时,堆栈的前几帧是什么?它似乎起源于 tbCodMaterial_TextChanged 还是其他地方?
  • 第一个 to frames 指向文本框,所以,是的,它似乎起源于 tbCodMaterial_TextChanged
猜你喜欢
  • 2020-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 2020-09-23
相关资源
最近更新 更多