如何在计时器功能中停止计时器?

时间:2012-03-08 14:31:29

标签: c# .net timer try-catch

我有一个计时器,我用它来制作一个char数组并每1秒执行一次其他任务,我需要使用try和catch方法检查这是否是一个错误。 如果发生错误,应显示一个警告消息框,我想立即停止计时器,所以我写了这个:

catch
        {
            MessageBox.Show(serialPort1.PortName + " is unavailable" + System.Environment.NewLine + "Please re-select the serial port", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            timer_read_M4.Stop();
        }

但计时器没有立即停止,消息框连续出现几次...... 一旦发生错误,我该怎么做才能停止计时器?

以下是计时器的代码

private void timer_read_M4_Tick(object sender, EventArgs e)
    {
        //-------------------------------------------------------------------------------------------------------------------------
        // read M4

        // if statement must be removed when testing on machine

        try
        {
            if (serialPort1.IsOpen == true)
            {                   
                rstr = sendCommand((string)("abc*"));
                rstr1 = responseText.Text;

                //convert to char array
                char[] receive1 = rstr1.ToCharArray(11, 4);
                char[] receive2 = rstr1.ToCharArray(15, 4);
                char[] receive3 = rstr1.ToCharArray(35, 4);
                char[] receive4 = rstr1.ToCharArray(7, 4);
                char[] receive5 = rstr1.ToCharArray(19, 4);
                char[] receive6 = rstr1.ToCharArray(23, 4);
                char[] receive7 = rstr1.ToCharArray(27, 4);
                char[] receive8 = rstr1.ToCharArray(31, 4);
                char[] receive9 = rstr1.ToCharArray(39, 4);

                //convert char array to string
                strm1 = new string(receive1);
                strm2 = new string(receive2);
                strm3 = new string(receive3);
                strm4 = new string(receive4);
                strm5 = new string(receive5);
                strm6 = new string(receive6);
                strm7 = new string(receive7);
                strm8 = new string(receive8);
                strm9 = new string(receive9);

                fyp.GlobalM8OutputFunction = strm9;     // carry M8 output to M8 page

                // convert string to binary to string
                bn1 = Convert.ToString(Convert.ToInt32(strm1, 16), 2);   // convert string to binary and display in textbox
                bn2 = Convert.ToString(Convert.ToInt32(strm2, 16), 2);   // convert string to binary and display in textbox(single)
                bn3 = Convert.ToString(Convert.ToInt32(strm3, 16), 2);   // convert string to binary and display in textbox
                bn4 = Convert.ToString(Convert.ToInt32(strm4, 16), 2);
                bn5 = Convert.ToString(Convert.ToInt32(strm5, 16), 2);   // convert string to binary and display in textbox
                bn6 = Convert.ToString(Convert.ToInt32(strm6, 16), 2);
                bn7 = Convert.ToString(Convert.ToInt32(strm7, 16), 2);   // convert string to binary and display in textbox
                bn8 = Convert.ToString(Convert.ToInt32(strm8, 16), 2);

                // stringArr.Text = bn4;
                // stringArr2.Text = bn7;
                // convert string to char array
                // convert binary to char array
                // char[] alarm1 = bn1.ToCharArray(3, 1);
                // char[] alarm2 = bn2.ToCharArray(3, 1);
                // char[] alarm3 = bn3.ToCharArray(9, 1);
                // char[] alarm4 = bn4.ToCharArray(3, 1);
                // char[] alarm5 = bn5.ToCharArray(3, 1);
                // char[] alarm6 = bn6.ToCharArray(3, 1);
                // here
                char[] alarm7 = bn7.ToCharArray(3, 1);
                // char[] alarm8 = bn8.ToCharArray(3, 1);

                // convert char array to string
                // strmb1 = new string(bn1);
                // strmb2 = new string(bn2);
                // strmb3 = new string(alarm3);
                // strmb4 = new string(bn4);
                // strmb5 = new string(bn5);
                // strmb6 = new string(bn6);
                // here
                strmb7 = new string(alarm7);
                // strmb8 = new string(bn8);

                stringArr.Text = bn1;
                stringArr2.Text = bn2;   // convert string to binary and display in textbox(single)
                txtm51.Text = bn3;   // convert string to binary and display in textbox
                txtm61.Text = bn4;
                txtm71.Text = bn5;   // convert string to binary and display in textbox
                stringArr3.Text = bn6;
                txtm52.Text = strmb7;   // convert string to binary and display in textbox
                txtm62.Text = bn8;
                txtm72.Text = strm9;
            }
        }
        catch
        {
            MessageBox.Show(serialPort1.PortName + " is unavailable" + System.Environment.NewLine + "Please re-select the serial port", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            timer_read_M4.Stop();
        }

1 个答案:

答案 0 :(得分:2)

你的MessageBox干扰停止计时器

写下这样的句子:

catch
{
    timer_read_M4.Stop();
    MessageBox.Show(serialPort1.PortName + " is unavailable" + System.Environment.NewLine + "Please re-select the serial port", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}