如何再次进入for循环?

dzone12345 2017-12-29 10:33:13
写了一下for循环,里面调用 了timer1,然后timer1里又调用了timer2,请问我怎么从timer2再回到for循环呢?

for (int i = 0; i < a.Length; i++)
{
a1 = Convert.ToByte(list2[i]);
a2 = Convert.ToByte(list2[i + 1]);
a3 = Convert.ToByte(list2[i + 2]);
a4 = Convert.ToByte(list2[i + 3]);

WriteByteToSerialPort(a1, a2, a3, a4);
WriteByteToSerialPort(b1, b2, b3, b4);
i = i + 3;

//延时
timer1.Interval = t1 * 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
//WriteByteToSerialPort(pump, openPump, );
Delay(100);
//WriteByteToSerialPort(pump, openPump, );
timer3.Interval = mt1 * 1000;
timer3.Start();
}

private void timer2_Tick(object sender, EventArgs e)
{
timer2.Stop();
//WriteByteToSerialPort(pump, closePump, );
Delay(100);
//WriteByteToSerialPort(pump, closePump, ); }
...全文
842 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2018-01-03
  • 打赏
  • 举报
回复
已经有 Timer t; 了 你再来个 int t = 2000; 你认为是对的吗?
xuzuning 2018-01-02
  • 打赏
  • 举报
回复
System.Windows.Forms.Timer 和 System.Threading.Timer 是同名的两个组件 他的代码中使用的是 后者,因为有冲突,所以你要指明是哪个 static Timer t; 要写作 static System.Threading.Timer t; 之后的 new 也要全称
秋的红果实 2018-01-02
  • 打赏
  • 举报
回复
static Timer t; ==> static System.Threading.Timer t;
dzone12345 2018-01-02
  • 打赏
  • 举报
回复
int t = 2000; static void x(int[] arr, int index) { Console.WriteLine("x--执行,index={0}", index); t = new Timer(h => y(arr, index), null, t, -1); } 这个红色的提示有错误,如何能够在函数内部使用这个t呢?
dzone12345 2018-01-02
  • 打赏
  • 举报
回复
引用 5 楼 sp1234 的回复:
我给你写一个控制台的例子:
using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static Timer t;

        static void Main(string[] args)
        {
            var a = new int[5] { 1, 2, 3, 4, 5 };
            t = new Timer(h => x(a, 0), null, 1000, -1);
            Console.WriteLine("................按任意键结束");
            Console.ReadKey();
        }

        static void x(int[] arr, int index)
        {
            Console.WriteLine("x--执行,index={0}", index);
            t = new Timer(h => y(arr, index), null, 500, -1);
        }

        static void y(int[] arr, int index)
        {
            Console.WriteLine("y--执行,index={0}", index);
            if (index < arr.Length)
                t = new Timer(h => x(arr, index + 1), null, 1500, -1);
            else
                Console.WriteLine("全部处理完毕,不再调用x了。");
        }
    }
}

你会看到,异步交互式的程序流程,根本没有什么简单函数 for 循环、while 循环那种变成概念,而是“委托、回调、事件”的编程设计概念。能够把流程倒置理解,这才正确地理解了更加真实地符合自然应用领域需求的异步操作流程。而那种顺序式的函数式编程,只是算个小数字的时候才用的,是刚学编程时都会学到的,但是不是交互式程序设计技术。
我在winform下有错误提示 错误 6 “Timer”是“System.Windows.Forms.Timer”和“System.Threading.Timer”之间的不明确的引用
  • 打赏
  • 举报
回复
你要在大脑中有一个直观的流程设计图像,知道这个操作过程完全是回调和数据驱动的,根本没有循环语句。不要把一些真实的流程当作循环,在实际开发中如果在主控制程序去写一个大大的循环语句,这往往被判定为实习生写的错误代码、没有认真从细节出发来设计系统事件驱动流程的代码!
  • 打赏
  • 举报
回复
lz 可能需要自动调整 t1、mt1 两个参数,他流程需求可能就是定时器为核心的。
  • 打赏
  • 举报
回复
你这不就是下面这种么,需要这样用timer么?
for()
{
 await WriteByteToSerialPort
 await WriteByteToSerialPort
}
  • 打赏
  • 举报
回复
随便一个 javascript 程序员都懂
setTimeout(func,interval)
异步操作的编程方法。一个 c# 程序员也应该熟练设计异步操作流程,不要只会写 for 循环。
  • 打赏
  • 举报
回复
我给你写一个控制台的例子:
using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static Timer t;

        static void Main(string[] args)
        {
            var a = new int[5] { 1, 2, 3, 4, 5 };
            t = new Timer(h => x(a, 0), null, 1000, -1);
            Console.WriteLine("................按任意键结束");
            Console.ReadKey();
        }

        static void x(int[] arr, int index)
        {
            Console.WriteLine("x--执行,index={0}", index);
            t = new Timer(h => y(arr, index), null, 500, -1);
        }

        static void y(int[] arr, int index)
        {
            Console.WriteLine("y--执行,index={0}", index);
            if (index < arr.Length)
                t = new Timer(h => x(arr, index + 1), null, 1500, -1);
            else
                Console.WriteLine("全部处理完毕,不再调用x了。");
        }
    }
}

你会看到,异步交互式的程序流程,根本没有什么简单函数 for 循环、while 循环那种变成概念,而是“委托、回调、事件”的编程设计概念。能够把流程倒置理解,这才正确地理解了更加真实地符合自然应用领域需求的异步操作流程。而那种顺序式的函数式编程,只是算个小数字的时候才用的,是刚学编程时都会学到的,但是不是交互式程序设计技术。
  • 打赏
  • 举报
回复
你首先处理 a[0],定义了几百毫秒延时之后操作调用某方法 x,然后再等几百毫秒之后调用某方法 y,然后处理 a[1],然后几百毫秒之后又调用某方法x........一直到 a 数组全部处理完毕。 这个流程根本没有 for 循环,怎么能写 for 语句?
zhuowp 2017-12-29
  • 打赏
  • 举报
回复
从代码上看,你的代码是有很大的问题的。感觉你对业务的实现方式是不对的。 与其讨论怎么回到for循环,还不如说说你想要做什么,然后重新设计一个实现方式。
  • 打赏
  • 举报
回复
根本不应该写 for 循环,这就不应该有 for 循环概念。 异步操作,当你要去市场上采购东西,当你跟人家达成交易之后你才应该应该付款,然后装车、拉走。你总不能闷着头把你看中的东西按照自己相像的价格扔点钱给人家、就强行拉走货物吧?! 异步处理的流程,你注册委托等待回调执行第一件任务,然后第一件任务结束时才注册委托等待回调执行第二件任务.........这要求脑子里有个异步、事件驱动的由数据和回调来完成流程概念,不是盲目地顺序 for 循环计算。 根本没有什么循环语句。
dzone12345 2017-12-29
  • 打赏
  • 举报
回复
写了一个for循环,里面调用 了timer1,然后timer1里又调用了timer2,请问我怎么从timer2再回到for循环呢?
SoulRed 2017-12-29
  • 打赏
  • 举报
回复
感觉可以用yield
Jason_Mao1 2017-12-29
  • 打赏
  • 举报
回复
我感觉你这个搞的有点复杂,为什么要这样搞呢。你把timer1 里面的那个方法独立出来 试试,好好想想的场景。把你的场景和解决思路好好规划一下 。
dzone12345 2017-12-29
  • 打赏
  • 举报
回复
[quote=引用 5 楼 sp1234 的回复:] 我给你写一个控制台的例子:
using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static Timer t;

        static void Main(string[] args)
        {
            var a = new int[5] { 1, 2, 3, 4, 5 };
            t = new Timer(h => x(a, 0), null, 1000, -1);
            Console.WriteLine("................按任意键结束");
            Console.ReadKey();
        }

        static void x(int[] arr, int index)
        {
            Console.WriteLine("x--执行,index={0}", index);
            t = new Timer(h => y(arr, index), null, 500, -1);
        }

        static void y(int[] arr, int index)
        {
            Console.WriteLine("y--执行,index={0}", index);
            if (index < arr.Length)
                t = new Timer(h => x(arr, index + 1), null, 1500, -1);
            else
                Console.WriteLine("全部处理完毕,不再调用x了。");
        }
    }
}

quote] 这里面“委托、回调、事件”在哪里体现,能给解释一下吗,非常感谢!
dzone12345 2017-12-29
  • 打赏
  • 举报
回复
引用 7 楼 starfd 的回复:
你这不就是下面这种么,需要这样用timer么?
for()
{
 await WriteByteToSerialPort
 await WriteByteToSerialPort
}
这个 await WriteByteToSerialPort 什么作用,麻烦给解释一下,谢谢!
xuzuning 2017-12-29
  • 打赏
  • 举报
回复
当然,如果 Interval 比较小,而循环时间有比较长,那么 Tick 事件是会在循环期间被触发的,循环将被中断,但绝不是你期望的 timer1.Start(); 处 Tick 事件处理结束后,循环从断点处继续运行,这也不是什么再次进入
xuzuning 2017-12-29
  • 打赏
  • 举报
回复
你先搞清楚 Timer 是什么 Timer 是定时器,执行 Start() 方法时开始计时,时间到触发 Tick 事件 所以你的代码: //延时 timer1.Interval = t1 * 1000; timer1.Start(); 并未达到延时的目的,他只是要在 t1 * 1000 之后做件什么事情 放在循环里的话,并不会影响循环的执行,所以并不存在 再次进入for循环 的问题,因为即使时间到的时候循环早就结束了 挂起程序用 Thread.Sleep 方法

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧