带有符号扫描程序的Windows Mobile应用程序在关闭应用程序时崩溃

时间:2013-12-04 10:31:54

标签: c# barcode-scanner motorola-emdk compact-framework2.0

我有一个问题,我无法提出解决方案。关于我在wm 6.5和Compact Framework 2.0上使用motorola MC3190-G手持应用程序的应用程序

在我的应用程序中实现使用扫描程序触发器的符号包之后,每次关闭我的应用程序/或启用了扫描程序的表单时,我都会收到以下错误: (即使我从未收到扫描数据)

我在stackoverflow和其他网站上搜索了一个没有解决方案的解决方案。

我是移动应用程序开发的新手。我尝试捕获一切,但错误似乎无法捕获异常类型“异常”。

抱歉英语不好。

错误:

  

的NullReferenceException

     

在MainForm.SymbolReader_ReadNotifiy()

Task.Invoke()
  在
System.Windows.Forms.Control._InvokeAll()

  System.Windows.Forms.Control.WinProc()

信息:

摩托罗拉EMDK 2.6 装置:MC3190-G

源码:

Scanner.cs

namespace MyScann
{
    /// <summary>
    /// Summary description for Scanner.
    /// </summary>
    public class Scanner
    {

        public static Symbol.Barcode.Reader SymbolReader = null;
        public static Symbol.Barcode.ReaderData SymbolReaderData = null;
        public static System.EventHandler SymbolEventHandler = null;
        public static DataSet CodeDataSet;


        //public static Utils.Sound ReadErrorSound;

        public static bool ScannerEnabled = false;
        public static void ActivateScanner()
        {

            // If we have both a reader and a reader-data Object
            if ( Scanner.SymbolReader != null &&
                Scanner.SymbolReaderData != null &&
                ! Scanner.SymbolReaderData.IsPending )
            {
                // Submit 'Read'
                try
                {
                    Scanner.SymbolReader.Actions.Read(Scanner.SymbolReaderData);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    if ( err.InnerException != null )
                        MessageBox.Show(err.InnerException.Message);
                }
            }

        }
        public static void DeactivateScanner()
        {

            // If we have both a reader and a reader-data Object
            if ( Scanner.SymbolReader != null &&
                Scanner.SymbolReaderData != null &&
                Scanner.SymbolReaderData.IsPending )
            {
                // Submit 'CancelRead'
                try
                {
                    Scanner.SymbolReader.Actions.CancelRead(Scanner.SymbolReaderData);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                    if ( err.InnerException != null )
                        MessageBox.Show(err.InnerException.Message);
                }
            }

        }

        /// <summary>
        /// Initialize the reader.
        /// </summary>
        /// <returns>False if an error accurs</returns>
        public static bool InitSymbolReader()
        {

            // If reader is already present then fail initialize
            if ( SymbolReader != null )
            {
                return false;

            }

            // Create new reader, first available reader will be used.
            SymbolReader = new Symbol.Barcode.Reader();
            // Create reader data
            SymbolReaderData = new Symbol.Barcode.ReaderData(
                Symbol.Barcode.ReaderDataTypes.Text,
                Symbol.Barcode.ReaderDataLengths.DefaultText);

            // set scanner read error sound
            //ReadErrorSound = new Utils.Sound(ErrorSound);

            return true;
        }

        /// <summary>
        /// Stop reading and disable/close reader
        /// </summary>
        public static void DeinitSymbolReader()
        {
            try
            {
                // If we have a reader
                if ( SymbolReader != null )
                {

                    //Cancel Incoming Requests
                    SymbolReader.Actions.Flush();

                    // Disable the reader
                    SymbolReader.Actions.Disable();

                    // Free it up
                    SymbolReader.Dispose();

                    // Indicate we no longer have one
                    SymbolReader = null;
                }

                // If we have a reader data
                if ( SymbolReaderData != null )
                {
                    // Free it up
                    SymbolReaderData.Dispose();

                    // Indicate we no longer have one
                    SymbolReaderData = null;
                }
                if(SymbolEventHandler!=null){
                    //SymbolEventHandler(this,null);
                    SymbolEventHandler = null;
                }
            }
            catch (Exception e){MessageBox.Show(e.StackTrace.ToString());
            }

        }

        /// <summary>
        /// Enable most barcode type with no limitation on length
        /// </summary>
        public static void OpenAllBarcodes()
        {
            SymbolReader.Decoders.CODABAR.Enabled = true;
            SymbolReader.Decoders.CODABAR.MinimumLength = 0;
            SymbolReader.Decoders.CODABAR.MaximumLength = 0;

            SymbolReader.Decoders.CODE11.Enabled = true;
            SymbolReader.Decoders.CODE11.MinimumLength = 0;
            SymbolReader.Decoders.CODE11.MaximumLength = 0;

            SymbolReader.Decoders.CODE39.Enabled = true;
            SymbolReader.Decoders.CODE39.MinimumLength = 0;
            SymbolReader.Decoders.CODE39.MaximumLength = 0;

            SymbolReader.Decoders.CODE93.Enabled = true;
            SymbolReader.Decoders.CODE93.MinimumLength = 0;
            SymbolReader.Decoders.CODE93.MaximumLength = 0;

            SymbolReader.Decoders.CODE128.Enabled = true;
            SymbolReader.Decoders.CODE128.MinimumLength = 0;
            SymbolReader.Decoders.CODE128.MaximumLength = 0;

            SymbolReader.Decoders.DATAMATRIX.Enabled = true;
            SymbolReader.Decoders.DATAMATRIX.MinimumLength = 0;
            SymbolReader.Decoders.DATAMATRIX.MaximumLength = 0;

            SymbolReader.Decoders.D2OF5.Enabled = true;
            SymbolReader.Decoders.D2OF5.MinimumLength = 0;
            SymbolReader.Decoders.D2OF5.MaximumLength = 0;

            SymbolReader.Decoders.I2OF5.Enabled = true;
            SymbolReader.Decoders.I2OF5.MinimumLength = 0;
            SymbolReader.Decoders.I2OF5.MaximumLength = 0;
            /*
        SymbolReader.Decoders.EAN13.Enabled = true;
        SymbolReader.Decoders.EAN13.MinimumLength = 0;
        SymbolReader.Decoders.EAN13.MaximumLength = 0;

        SymbolReader.Decoders.EAN8.Enabled = true;
        SymbolReader.Decoders.EAN8.MinimumLength = 0;
        SymbolReader.Decoders.EAN8.MaximumLength = 0;

        SymbolReader.Decoders.PDF417.Enabled = true;
        SymbolReader.Decoders.PDF417.MinimumLength = 0;
        SymbolReader.Decoders.PDF417.MaximumLength = 0;

        SymbolReader.Decoders.UPCA.Enabled = true;
        SymbolReader.Decoders.UPCA.MinimumLength = 0;
        SymbolReader.Decoders.UPCA.MaximumLength = 0;

        SymbolReader.Decoders.UPCE1.Enabled = true;
        SymbolReader.Decoders.UPCE1.MinimumLength = 0;
        SymbolReader.Decoders.UPCE1.MaximumLength =0;

        SymbolReader.Decoders.UPCE0.Enabled = true;
        SymbolReader.Decoders.UPCE0.MinimumLength = 0;
        SymbolReader.Decoders.UPCE0.MaximumLength = 0;
             */




            SymbolReader.Parameters.CodeIdType = Symbol.Barcode.CodeIdTypes.None;
            SymbolReader.Parameters.ScanType = Symbol.Barcode.ScanTypes.Background;
            SymbolReader.Parameters.LocalFeedback = Symbol.Barcode.DisabledEnabled.Enabled;

            Scanner.SymbolReader.Parameters.Feedback.Success.BeepTime = 0;
        }







    }
}
}

在MainForm.cs内部

    public void InitScanner(byte BatteryLevel)
    {

        // Create event handler delegate
        if (Scanner.MyScann.Scanner.SymbolEventHandler == null)
        {
            Scanner.MyScann.Scanner.SymbolEventHandler = new EventHandler(this.SymbolReader_ReadNotify);
            // Enable reader, with wait cursor
            Scanner.MyScann.Scanner.SymbolReader.Actions.Enable();
            Scanner.MyScann.Scanner.OpenAllBarcodes();
            Scanner.MyScann.Scanner.ScannerEnabled = true;
        }
        // If we have both a reader and a reader data
        if ((Scanner.MyScann.Scanner.SymbolReader != null) &&
            (Scanner.MyScann.Scanner.SymbolReaderData != null))
        {
            // Submit a read
            Scanner.MyScann.Scanner.SymbolReader.ReadNotify += Scanner.MyScann.Scanner.SymbolEventHandler;
            Scanner.MyScann.Scanner.SymbolReader.Actions.Read(Scanner.MyScann.Scanner.SymbolReaderData);
        }
    }

    public void SymbolReader_ReadNotify(object sender, EventArgs e)
    {
        Symbol.Barcode.ReaderData TheReaderData = Scanner.MyScann.Scanner.SymbolReader.GetNextReaderData();
        if (TheReaderData.Result == Symbol.Results.SUCCESS /*&& (txtBarcode.Focused == true)*/)
        {

            //  if (txtBarcode.Focused == true)
            //  {
            //       txtBarcode.Text = TheReaderData.Text.ToString();
            MessageBox.Show(TheReaderData.Text.ToString());
            SymbolReader_CycleScannerReader();
            return;
            //   }
        }
        SymbolReader_CycleScannerReader();
    }

    public void SymbolReader_CycleScannerReader()
    {
        Scanner.MyScann.Scanner.SymbolReader.Actions.Read(Scanner.MyScann.Scanner.SymbolReaderData);
    }

    public void StartScanner()
    {
        bool flag = false;
        try
        {
            Scanner.MyScann.Scanner.SymbolEventHandler = null;
            Scanner.MyScann.Scanner.DeinitSymbolReader();
            Scanner.MyScann.Scanner.ScannerEnabled = false;
            if (!Scanner.MyScann.Scanner.ScannerEnabled)
            {
                Scanner.MyScann.Scanner.SymbolEventHandler = null;
                Scanner.MyScann.Scanner.InitSymbolReader();
                InitScanner(100);
                flag = true;
            }
        }
        catch
        {
            Scanner.MyScann.Scanner.SymbolEventHandler = null;
            Scanner.MyScann.Scanner.InitSymbolReader();
            InitScanner(100);
            flag = true;
        }
        finally
        {
            if (!flag)
            {
                MessageBox.Show("Scanner Error");
            }
        }
    }

    void public void CloseScanner()
    {
        Scanner.MyScann.Scanner.SymbolEventHandler = null;
        Scanner.MyScann.Scanner.DeinitSymbolReader();
    }

void MainFormLoad(object sender, EventArgs e)
    {
        StartScanner();
    }
void MainFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
    {
            CloseScanner();
    }
    void ButtonExitClick(object sender, EventArgs e)
    {
        Scanner.MyScann.Scanner.DeinitSymbolReader();
        this.Close();
        Application.Exit();
    }

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

我不熟悉符号库,但从我可以看到的问题是在扫描仪的“取消初始化”后的某个时刻出现 ReadNotify 事件。

您应该在 MainForm.CloseScanner()中执行的操作是先删除 MainForm.InitScanner()中添加的所有句柄,然后调用 Scanner .DeinitSymbolReader()

编辑: 您应该使用 - = 运算符删除句柄,而不是指定空引用。

希望这有帮助。

答案 1 :(得分:0)

尝试在退出时不调用'DeinitSymbolReader',看看是否修复了它

皮特

答案 2 :(得分:0)

我的印象是,使用或不使用'deinitSymbolReader'时,您的应用程序无法正确退出。通过在已经关闭的资源上调用deinitSymbolReader可能会导致访问冲突。 也许您应该尝试放弃deinintSymbolReader,并尝试在Form的Disposing Event中处理扫描仪/触发器。

似乎问题在于您正在处理复杂的数据结构,而且您正在将它们更改为null,这基本上意味着您正在更改引用,并且可能确保GC永远不会释放所有未经管理的资源。

您正在处理对未受管理资源的不当操作的一个迹象是,第二个deinintSymbolReader不应该执行任何操作(因为所有资源都为null),因此更少导致错误。

我有一个使用触发器和扫描程序的应用程序,如果扫描程序不为空,它只需要“enableScanner = false”和“Scanner.Dispose()”。