创建倒数计时器,直到计时器启动

时间:2015-09-29 14:08:08

标签: c#

我在控制台应用中有一个计时器:

using System.Timers;
Timer Timer = new Timer();

我给了它一个interval,它定期用_timer_Elapsed方法填写:

Timer.Elapsed += _timer_Elapsed;
Timer.Enabled = true;

private static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
    ...
}

如何创建第二个计时器,直到此计时器开始倒计时?

2 个答案:

答案 0 :(得分:0)

创建第二个计时器,该计时器每秒都会过去,并使用以下代码倒计时并向控制台发送写入消息。

#define COUNTDOWN_SECONDS 10

private int CountDownValue = COUNTDOWN_SECONDS;

private static void _timer2_Elapsed(object sender, ElapsedEventArgs e)
{
    WriteConsoleMessage(CountDownValue--);

    if (CountDownValue == 0)
    {
        // Stop timer2
        // Start timer1
    }
}

private static void WriteConsoleMessage(int Value)
{
    if (Value < COUNTDOWN_SECONDS)
        Console.CursorLeft = 0;  // Reset cursor to start of the line

    Console.Write(string.Format("{0} Seconds until timer starts", Value.ToString());
}

答案 1 :(得分:0)

继承人我是怎么做到的:

    public static int Interval = 5000;
    public static int IntervalLeft = Interval;


            Timer.Elapsed += _timer_Elapsed;
            Timer.Enabled = true;
            Timer.Interval = Interval;

            CountDownTimer.Elapsed += _CountDowntimer_Elapsed;
            CountDownTimer.Enabled = true;
            CountDownTimer.Interval = 1000;
            CountDownTimer.Start();



    private static void _timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        CountDownTimer.Stop();
        Timer.Stop();

         DOES THE JOB HERE


        Timer.Start();
        CountDownTimer.Start();
        IntervalLeft = Interval;
    }


   private static void _CountDowntimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Zipper.ClearCurrentConsoleLine();
        IntervalLeft = (IntervalLeft - 1000);
        Console.Write("Starts in" + IntervalLeft/1000);
    }

第一个计时器在经过时停止第二个计时器...