private void cycle()
{
  for (i=0;i<=100;i++)
   {
     ....
     timer1.Interval = 5000;
     timer1.start();
   }
}private void timer1_Tick(object sender, EventArgs e)
{
   if (满足某种条件)
    ???
}
我希望的是,满足条件进行下一步循环,或时间到也进行下一步循环,应该怎么写呢?

解决方案 »

  1.   

    问题说清楚点行么?
     if (满足某种条件) 
        {
               cycle()
        }把timer1.start.start()从cycle函数中移出来,到别的地方去
      

  2.   

    哦,我是做一个串口操作
    Serialport1串口控件,sp1
    当timer1.start()的时候sp1也开始接收数据如果他接满了多少位数据则进入下一步循环
    但由于可能有参数设置错误的情况,接不到数据,那么就定了5秒做为超时进入下一步循环
      

  3.   

    ydsunny,你有qq么,加个吧,76502681
      

  4.   

    string myrecieve;
    string mystring;private void cycle() //自定义的函数
    {
      int[] arrayboudrate = new int[9] { 300,1200,2400,4800,115200,9600,19200,38400,57600};
      for (i=0;i <=100;i++) 
       {      sp1.BaudRate = arrayboudrate[i1];
         .... //设置串口参数
         try
         {
           sp1.open();//打开串口,开始接收数据
         }catch{}
         timer1.Interval = 5000; 
         timer1.start(); 
          if (超过时间5秒)
           {
             continue;
           }
          else
           {
             if (接收的字符串与协议匹配)
                 终止循环
               else
                 continue;
           }
       } 
    } private void timer1_Tick(object sender, EventArgs e) 

       这里应该怎么写
        ??? 
    } private void sp1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
     {
                myrecieve = sp1.ReadExisting();
                if (mystring.Length < 40)
                    mystring = mystring + myrecieve;
     }
      

  5.   

    感觉你用timer控制是不行的,不知道你用的什么东西接收的数据,socket上是可以设置超时时间的,这个超时5秒也应该是在socket上设的
      

  6.   

    这样描述还不够么?哪里看不懂,我再写详细些我用的是System.IO.Ports.Serial
    单一超时好办,但是在这个函数里,即要打开sp,又要启动timer,然后超时还要再反馈回来
    不会写,大侠帮帮忙
      

  7.   


    private void cycle() //自定义的函数 

      int[] arrayboudrate = new int[9] { 300,1200,2400,4800,115200,9600,19200,38400,57600}; 
      for (i=0;i  <=100;i++)  
       {       sp1.BaudRate = arrayboudrate[i1]; 
         //.... //设置串口参数 
         try 
         { 
            sp1.open();//打开串口,开始接收数据         timer1.Enabled=false;//如果正常,时间控件为无效状态         //在这里写代码判断是否接满了你所说的多少位     }
         catch
         {        timer1.Enabled=true;//发生错误,时间控件启动
            timer1.Interval = 5000;
          
         } 
      }
    }  private void timer1_Tick(object sender, EventArgs e)  
    {
        cycle();//执行该方法
    }
      

  8.   

    额这里时间控件我不是用来处理sp1.open的异常情况的
    我用timer1的目的是因为,波特率不对的话,可能会一直接到空数据
    也就是说我是要在sp1.open正常后开始计时5秒那么也就不能在timer1_Tick里执行cycle()了。。不知道我想法错哪了。。
      

  9.   


        bool iscontinue = false;
        private void cycle() //自定义的函数 

      int[] arrayboudrate = new int[9] { 300,1200,2400,4800,115200,9600,19200,38400,57600}; 
      for (i=0;i  <=100;i++)  
       {       sp1.BaudRate = arrayboudrate[i1]; 
         .... //设置串口参数 
         try 
         { 
             iscontinue = false;
             timer1.Interval = 5000;  
             timer1.start();
            sp1.open();//打开串口,开始接收数据 
         }
         catch
         {
             iscontinue = true;
         }
          if(iscontinue) continue;
          else
           { 
             if (接收的字符串与协议匹配) 
                 终止循环 
               else 
                 continue; 
           } 
       }  
    }  private void timer1_Tick(object sender, EventArgs e)  
    {  
        iscontinue = true;
          timer1.stop();
    }