C#新线程延时

开启一个新线程;

在这个线程中,进行任务排队。

任务1完成后,等待延时200ms,再运行任务2

 private void Timer1_Tick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Task.Run(() =>
            {
            
                this.Invoke( new Action( () => 
                {
                    listBox1.Items.Add("进中断"+DateTime.Now.ToString() + "\r\n");
                }));
                //RS485.Set_io(7);//ok
                //RS485.Rest_io(7);//ok
                if (i > 8) i = 0;
                RS485.Set_io(i++);//ok
                this.Invoke(new Action(() =>
                {
                    listBox1.Items.Add("第1次输出" + DateTime.Now.ToString() + "\r\n");
                }));


                Thread.Sleep(200);
                RS485.Rest_io((ushort)(i - 2));//ok                                           
                this.Invoke(new Action(() =>
                {
                    listBox1.Items.Add("第2次输出" + DateTime.Now.ToString() + "\r\n");
                }));


                Thread.Sleep(200);
                //RS485.Read_io_out(0,8);//ok
                RS485.Read_io_in(0, 8);//ok
                this.Invoke(new Action(() =>
                {
                    listBox1.Items.Add("第3次输出" + DateTime.Now.ToString() + "\r\n");
                }));
                //RS485.Read_io_Reg(0,4);//
                //RS485.Read_io_Regs(0, 6);//
                Thread.Sleep(200);
            });
          
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值