同一类方法中的事件处理程序

时间:2012-10-28 06:05:40

标签: c#

我正在使用C#,。Net 2.0和VS2008开发我的项目。 简而言之,我的应用程序通过USB与设备通信。我处理来自设备的传入消息时遇到问题。

这是事件的处理程序“我们从设备获得了一些东西”:

private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)

它从未调用过同一个类的方法(我使用的是分类):

private void btn_indication_start_Click(object sender, EventArgs e)
{
            currentTest = "indication";
            this.ControlBox = false;

            bool isTestPassed = false;

            int timeout = 1000;

            sendMessageToDevice("A9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"); //device should respond to this message

            System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
            while (true)
            {
                if (stopwatch.ElapsedMilliseconds >= timeout)
                    break;
                Thread.Sleep(1);
            }

            if (!newMessageArrived)  //this bool variable always false :(
            {
                MessageBox.Show("Timeout!"); //usb_OnDataRecieved called only in this block! And newMessageArrived become true
                                             //and when we remove MessageBox, it don't called ever!
                this.ControlBox = true;
                currentTest = "";
                return;
            }

            //do something else with device...

            this.ControlBox = true;
            currentTest = "";
        }

如何使用事件处理程序修复此问题?

P.S。当我通过表单的按钮和文本框发送/接收消息时,它工作正常:

private void btn_send_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.tb_send.Text + " ";
                text.Trim();
                string[] arrText = text.Split(' ');
                byte[] data = new byte[arrText.Length];

                for (int i = 0; i < arrText.Length; i++)
                {
                    if (arrText[i] != "")
                    {
                        int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.HexNumber);
                        data[i] = (byte)Convert.ToByte(value);
                    }
                }

                if (this.usb.SpecifiedDevice != null)
                {
                    this.usb.SpecifiedDevice.SendData(data);
                }
                else
                {
                    MessageBox.Show("Device not found, plug it");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

private void usb_OnDataRecieved(object sender, DataRecievedEventArgs args)
        {
            if (InvokeRequired)
            {
                try
                {
                    Invoke(new DataRecievedEventHandler(usb_OnDataRecieved), new object[] { sender, args });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            else
            {

                string rec_data = "Data: ";
                foreach (byte myData in args.data)
                {
                    if (myData.ToString("X").Length == 1)
                    {
                        rec_data += "0";
                    }

                    rec_data += myData.ToString("X") + " ";
                }

                this.lb_read.Items.Insert(0, rec_data);
                lastMessageFromUsb = rec_data;
                newMessageArrived = true;
            }
        }

0 个答案:

没有答案