多个计时器

时间:2015-08-27 13:56:46

标签: c# timer

    private void timer5_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("sendmail");
        timer5.Enabled = false;
    }


    private void timer2_Tick(object sender, EventArgs e)
    {

               MessageBox.Show("timer2");
               timer2.Enabled = false;

               timer5.Interval = 5000;
               timer5_Tick(null, new EventArgs());
    }

    private void timer3_Tick(object sender, EventArgs e)
    {


            MessageBox.Show("timer3");

           timer3.Enabled = false;
           timer5.Interval = 5000;
           timer5_Tick(null, new EventArgs());
    }

    private void timer4_Tick(object sender, EventArgs e)
    {

            MessageBox.Show("timer4");
            timer5.Interval = 5000;
            timer5_Tick(null, new EventArgs());

        timer4.Enabled = false;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer2.Enabled = true;
        timer3.Enabled = true;
        timer4.Enabled = true;
        timer5.Enabled = false;
 }

以上代码有效:5secondwait> message-timer2,message-sendmail(同一时间)> 5secondwait message-timer3,message-sendmail(同一时间)> 5seconds> message-timer4,message-sendmail(同一时间)完成工作。 问题是:5secondwait> message-timer2> 5secondwait> message-sendmail> 5secondwait> message3timer3> 5secondswait> message-sendmail> 5seconswait> message-timer4> 5secondswait> message-sendmail。工作完成。 怎么做到这个?

1 个答案:

答案 0 :(得分:0)

我只想提供一个可能对您有帮助的简单代码段:

private async void DoStuff()
{
    MessageBox.Show("action1");
    await Task.Delay(4000),
    MessageBox.Show("action2");
    await Task.Delay(4000);
    //...
}