2012-03-08 6 views
2

나는 char 배열을 maniuplate하고 매 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(); 
     } 
+0

언어? 이게 C++일까요? –

+0

Visual C#, 미안하지만 일찍 언급하지 않음 =) – Ran

+3

MessageBox.Show()를 호출하기 전에 Stop() 메서드로 호출을 이동하려고 했습니까? –

답변

2

이 같은 캐치 쓰기 타이머

을 중지 방해 귀하의 메시지 박스 :

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); 

} 
+1

'쇼'전에 정류장이 와야합니다. –

+0

그게 무슨 뜻인지 .. 고마워. –

관련 문제