读书人

大家帮忙解决个PDA程序开发上的一个有

发布时间: 2012-06-06 16:44:11 作者: rapoo

大家帮忙解决个PDA程序开发上的一个问题
在PDA上写了个条码扫描功能,PDA系统是WinCE出了点问题大家帮忙看看:

C# code
using System;using System.Drawing;using System.Collections;using System.Windows.Forms;using System.Data;using System.Text;using MEC.Nurse.Provider;namespace RFIDDemo{    /// <summary>    /// Form1 的摘要说明。    /// </summary>    public class Form1 : System.Windows.Forms.Form    {        private System.Windows.Forms.Button btnStart;        private System.Windows.Forms.Button btnStop;        private System.Windows.Forms.TextBox txtData;        private System.Windows.Forms.Button btnClearText;        private System.Windows.Forms.Button btnStartRead;        private System.Windows.Forms.Button btnReadStop;        private System.Windows.Forms.Label lblInfo;        public Form1()        {            //            // Windows 窗体设计器支持所必需的            //            InitializeComponent();            //            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码            //            // 绑定函数到OnDataAttrived事件上            // 在读取到RFID数据后会回调绑定到该事件上的函数            RFID.GetInstance().OnDataArrived += new RFIDEventHandler(OnDataArrived);            // 获取rfid服务的状态            if (RFID.GetInstance().GetServerState())            {                                StartEnable();                lblInfo.Text = "RFID服务已启动!";            }            else            {                if(RFID.GetInstance().Start())                {                    StartEnable();                    lblInfo.Text = "RFID服务启动成功!";                    MessageBox.Show("aaaa");                }                else                {                    StopEnable();                    lblInfo.Text = "RFID服务启动失败!";                }            }        }        /// <summary>        /// 清理所有正在使用的资源。        /// </summary>        protected override void Dispose( bool disposing )        {            base.Dispose( disposing );        }        #region Windows 窗体设计器生成的代码        /// <summary>        /// 设计器支持所需的方法 - 不要使用代码编辑器修改        /// 此方法的内容。        /// </summary>        private void InitializeComponent()        {            this.btnStart = new System.Windows.Forms.Button();            this.btnStop = new System.Windows.Forms.Button();            this.txtData = new System.Windows.Forms.TextBox();            this.btnClearText = new System.Windows.Forms.Button();            this.lblInfo = new System.Windows.Forms.Label();            this.btnReadStop = new System.Windows.Forms.Button();            this.btnStartRead = new System.Windows.Forms.Button();            //             // btnStart            //             this.btnStart.Location = new System.Drawing.Point(16, 8);            this.btnStart.Size = new System.Drawing.Size(88, 24);            this.btnStart.Text = "启动";            this.btnStart.Click += new System.EventHandler(this.btnStart_Click);            //             // btnStop            //             this.btnStop.Location = new System.Drawing.Point(120, 8);            this.btnStop.Size = new System.Drawing.Size(88, 24);            this.btnStop.Text = "停止 ";            this.btnStop.Click += new System.EventHandler(this.btnStop_Click);            //             // txtData            //             this.txtData.Location = new System.Drawing.Point(16, 72);            this.txtData.Multiline = true;            this.txtData.Size = new System.Drawing.Size(192, 152);            this.txtData.Text = "";            //             // btnClearText            //             this.btnClearText.Location = new System.Drawing.Point(16, 232);            this.btnClearText.Size = new System.Drawing.Size(72, 32);            this.btnClearText.Text = "清空";            this.btnClearText.Click += new System.EventHandler(this.btnClearText_Click);            //             // lblInfo            //             this.lblInfo.Location = new System.Drawing.Point(96, 240);            this.lblInfo.Size = new System.Drawing.Size(128, 20);            //             // btnReadStop            //             this.btnReadStop.Location = new System.Drawing.Point(120, 40);            this.btnReadStop.Size = new System.Drawing.Size(88, 24);            this.btnReadStop.Text = "停止读取";            this.btnReadStop.Click += new System.EventHandler(this.btnReadStop_Click);            //             // btnStartRead            //             this.btnStartRead.Location = new System.Drawing.Point(16, 40);            this.btnStartRead.Size = new System.Drawing.Size(88, 24);            this.btnStartRead.Text = "开始读取";            this.btnStartRead.Click += new System.EventHandler(this.btnStartRead_Click);            //             // Form1            //             this.ClientSize = new System.Drawing.Size(226, 268);            this.Controls.Add(this.btnReadStop);            this.Controls.Add(this.btnStartRead);            this.Controls.Add(this.lblInfo);            this.Controls.Add(this.btnClearText);            this.Controls.Add(this.txtData);            this.Controls.Add(this.btnStop);            this.Controls.Add(this.btnStart);            this.Text = "RFID示例";            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);            this.Load += new System.EventHandler(this.Form1_Load);            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);        }        #endregion        /// <summary>        /// 应用程序的主入口点。        /// </summary>        static void Main()         {            Application.Run(new Form1());        }                /// <summary>        /// 打开服务        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnStart_Click(object sender, System.EventArgs e)        {            if (RFID.GetInstance().GetServerState())            {                StartEnable();                lblInfo.Text = "RFID服务启动成功!";                            }            else            {                if(RFID.GetInstance().Start())                {                    StartEnable();                    lblInfo.Text = "RFID服务启动成功!";                    MessageBox.Show("aaaa");                }                else                {                    StopEnable();                    lblInfo.Text = "RFID服务启动失败!";                }            }        }        /// <summary>        /// 关闭rfid服务        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnStop_Click(object sender, System.EventArgs e)        {            if (!RFID.GetInstance().GetServerState())            {                StopEnable();                lblInfo.Text = "RFID服务关闭成功!";            }            else            {                if(RFID.GetInstance().Stop())                {                    StopEnable();                    lblInfo.Text = "RFID服务关闭成功!";                }                else                {                    StartEnable();                    lblInfo.Text = "RFID服务关闭失败!";                }            }        }            private void StartEnable()        {            btnStart.Enabled = false;            btnStop.Enabled = true;        }        private void StopEnable()        {            btnStart.Enabled = true;            btnStop.Enabled = false;        }        private void SetText()        {            string strBak = txtData.Text;            if (strBak.Length < 1000)            {                txtData.Text = strText + strBak;            }            else            {                txtData.Text = strText;            }        }        string strText = string.Empty;        /// <summary>        /// 接收读取到的rfid数据        /// </summary>        /// <param name="args"></param>        public void OnDataArrived(MEC.Nurse.Provider.RFIDEventArgs args)        {            StringBuilder strData = new StringBuilder();            strData.Append("State : ");            strData.Append(args.StateCode);            strData.Append("\r\nData  : ");            strData.Append(args.Data);            strData.Append("\r\n---------------------\r\n");            ControlInvokeHandler action = new ControlInvokeHandler(SetText);            MessageBox.Show(strData.ToString());            txtData.Text = strData.ToString();            this.Invoke(action);        }        private delegate void ControlInvokeHandler();        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)        {            e.Cancel = true;            btnStop_Click(null, null);            this.Hide();            base.OnClosing (e);        }        private void btnClearText_Click(object sender, System.EventArgs e)        {            txtData.Text = string.Empty;        }        /*         * ReadStart和ReadEnd组合读取rfid          *          * 例如当手指按下按钮的时候ReadStart, 当手指离开按钮时ReadEnd         * 这样模块就会自动执行一次读取         * */        /// <summary>        /// 开始读取rfid数据        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnStartRead_Click(object sender, System.EventArgs e)        {            RFID.GetInstance().ReadStart();            StartReadEnable();        }        /// <summary>        /// 结束rfid数据读取        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnReadStop_Click(object sender, System.EventArgs e)        {            RFID.GetInstance().ReadEnd();            StopReadEnable();        }        private void StartReadEnable()        {            btnStartRead.Enabled = false;            btnReadStop.Enabled = true;        }        private void StopReadEnable()        {            btnStartRead.Enabled = true;            btnReadStop.Enabled = false;        }        private void Form1_Load(object sender, System.EventArgs e)        {                }        private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)        {            this.Focus();            if (e.KeyValue==122)            {                RFID.GetInstance().ReadStart();                StartReadEnable();            }        }        private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)        {            this.Focus();            if (e.KeyValue==122)            {                RFID.GetInstance().ReadEnd();                StopReadEnable();            }        }    }} 


现在有三个模块,在这三个模块内都有上面这些代码,现在发的问题是当我在第一个模块内执行这个代码后,关闭窗口进入另一个模块居然调用的是第一个模块内OnDataArrived方法内的代码,感觉在关闭第一个窗口的时候没有把这个条码扫描给结束了,只是停止了,然后当进入第二个模块的时候调用条码扫描功能把第一个条码扫描又给打开了,感觉始终老在操作一个东西,关闭这个程序,双击程序图标后如果我进第二个模块执行条码扫描,则第一个模块和第三个模块又都调用的第二个模块的东西!大家帮帮忙看看!!不知道这个涉及到线程吗,如果涉及到线程的话是不是老操作的一个线程,所以才出现这种问题?我的开发环境是VS2003版本太老了!!

[解决办法]
RFID.GetInstance().OnDataArrived += new RFIDEventHandler(OnDataArrived);

可能是每个窗体都绑定了事件。所以有数据时,每个窗体都要执行一次!

建议:
关闭窗体时,减去事件!

读书人网 >C#

热点推荐