如何在c#中同步两个定时器 [英] How to sync two timers in c#

查看:585
本文介绍了如何在c#中同步两个定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体应用程序中有两个计时器。



我希望第一个timer1通过button1点击启用。



我想知道的是如何做以下事项:



第一个计时器1必须点火一次(勾选一次)。



第二个计时器2在计时器1打勾20秒后启动。



谢谢。

I have two timers in a windows form app.

I want first timer1 to enable by button1 click.

What i want to know is how to do following:

The first timer1 must fire once(tick once).

And the second timer2 to start after 20 seconds of timer1 tick.

Thank you.

推荐答案

1。使用sleep()方法在20秒后启动timer1中的第二个计时器。

2.停止计时器2中的timer1。



1. Start the second timer in timer1 after 20 seconds using sleep() method.
2. Stop the timer1 in timer2.

int count = 1;

       private void button1_Click(object sender, EventArgs e)
       {
           timer1.Start();
           textBox1.Text = "Button1 Clicked at " + DateTime.Now.ToString();

       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           if (count == 1)
           {
               textBox1.Text = textBox1.Text + Environment.NewLine + "Timer1 Enabled at " + DateTime.Now.ToString();
           }

           if (count == 17)
           {
               timer2.Start();
           }
           count++;
       }

       private void timer2_Tick(object sender, EventArgs e)
       {
           timer1.Stop();
           textBox1.Text = textBox1.Text + Environment.NewLine + "Timer2 Enabled at " + DateTime.Now.ToString();

       }
   }







timer1的间隔为1000和timer2是4000。




Interval of timer1 is 1000 and timer2 is 4000.


一个好的开始是看计时器类 [ ^ ]并查看示例。它将为您提供如何使其工作的好主意。
A good start is to look at the Timer Class[^] and see the examples. It will give you a good idea on how-to make it work.


这篇关于如何在c#中同步两个定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆