各位程序员好!
我正在为具有条形码阅读器硬件的 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);
}
}
}