C# 暂停程序执行直到计时器停止

C# 暂停程序执行直到计时器停止,c#,timer,C#,Timer,我想在清除控制台窗口并显示更多文本之前,在控制台窗口中生成一些文本一段时间。我原以为下面的代码可以做到这一点,但在计时器过期后,控制台被清除,但没有显示其他文本 这是在计时器过期之前停止程序执行的正确方法吗 static void Main (string [] args) { const int PEEK_TIME = 5; const int SECOND = 1000; Timer peekTimer = new Timer (SECOND * PEEK_TIME

我想在清除控制台窗口并显示更多文本之前,在控制台窗口中生成一些文本一段时间。我原以为下面的代码可以做到这一点,但在计时器过期后,控制台被清除,但没有显示其他文本

这是在计时器过期之前停止程序执行的正确方法吗

static void Main (string [] args)
{
    const int PEEK_TIME = 5;
    const int SECOND = 1000;

    Timer peekTimer = new Timer (SECOND * PEEK_TIME);
    peekTimer.Elapsed += onTimerTick;
    peekTimer.AutoReset = false;

    peekTimer.Start ();
    Console.WriteLine ("Timer started...");
    while (peekTimer.Enabled) { }
    Console.WriteLine ("Timer done.");
    Console.ReadLine ();
}

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
    Console.Clear ();
}

onTimerTick
事件中,如果希望
while
循环退出,则需要停止计时器(或设置
peek timer.Enabled==false
):

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
    Console.Clear ();
    peekTimer.Stop();
}
为此,您必须在更大范围内声明计时器,以便可以从主方法和Tick事件处理程序访问它:

namespace ConsoleApplication
{
    timer peekTimer;

    public class Program
    {
回答你的大问题,这不是最好的方法。如果你只是坐着,在一段特定的时间里什么也不做,你最好把线放在睡眠中,而不是在整个时间里转来转去:

Console.WriteLine("Waiting 5 seconds...");
Thread.Sleep(TimeSpan.FromSeconds(5));
Console.WriteLine("...Time's up!!");

onTimerTick
事件中,如果希望
while
循环退出,则需要停止计时器(或设置
peek timer.Enabled==false
):

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
    Console.Clear ();
    peekTimer.Stop();
}
为此,您必须在更大范围内声明计时器,以便可以从主方法和Tick事件处理程序访问它:

namespace ConsoleApplication
{
    timer peekTimer;

    public class Program
    {
回答你的大问题,这不是最好的方法。如果你只是坐着,在一段特定的时间里什么也不做,你最好把线放在睡眠中,而不是在整个时间里转来转去:

Console.WriteLine("Waiting 5 seconds...");
Thread.Sleep(TimeSpan.FromSeconds(5));
Console.WriteLine("...Time's up!!");

您不需要计时器:

static void Main (string [] args)
{
    const int PEEK_TIME = 5;
    const int SECOND = 1000;

    var task = Task.Run(()=> 
        {
            Console.WriteLine ("Work started...");
            Thread.Sleep(PEEK_TIME*SECOND);
            Console.Clear();
        });
    task.Wait();
    Console.WriteLine ("Work done.");
    Console.ReadLine ();
}

您不需要计时器:

static void Main (string [] args)
{
    const int PEEK_TIME = 5;
    const int SECOND = 1000;

    var task = Task.Run(()=> 
        {
            Console.WriteLine ("Work started...");
            Thread.Sleep(PEEK_TIME*SECOND);
            Console.Clear();
        });
    task.Wait();
    Console.WriteLine ("Work done.");
    Console.ReadLine ();
}

下面是使用Thread.Sleep函数的代码示例

using System;
using System.Threading;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Timer Start!");

            Thread.Sleep(5000);
            Console.Clear();
            Console.WriteLine("End");

            Console.ReadKey();
        }
    }
}

下面是使用Thread.Sleep函数的代码示例

using System;
using System.Threading;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Timer Start!");

            Thread.Sleep(5000);
            Console.Clear();
            Console.WriteLine("End");

            Console.ReadKey();
        }
    }
}

在我的游戏中,我使用这种方法:

public static Timer peekTimer = new Timer (SECOND * PEEK_TIME);

static void Main (string [] args)
{
const int PEEK_TIME = 5;
const int SECOND = 1000;    

Console.WriteLine ("Timer started...");
peekTimer.Interval = new TimeSpan(0, 0, 0, 0, SECOND * PEEK_TIME);
peekTimer.Tick += new EventHandler(onTimerTick);
peekTimer.Start();

}

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
peekTimer.Stop();
Console.Clear ();
Console.WriteLine ("Timer done.");
Console.ReadLine ();
}

在我的游戏中,我使用这种方法:

public static Timer peekTimer = new Timer (SECOND * PEEK_TIME);

static void Main (string [] args)
{
const int PEEK_TIME = 5;
const int SECOND = 1000;    

Console.WriteLine ("Timer started...");
peekTimer.Interval = new TimeSpan(0, 0, 0, 0, SECOND * PEEK_TIME);
peekTimer.Tick += new EventHandler(onTimerTick);
peekTimer.Start();

}

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
peekTimer.Stop();
Console.Clear ();
Console.WriteLine ("Timer done.");
Console.ReadLine ();
}

除非有未说明的要求,否则这就是OP的全部需求。所有这些任务/多线程/计时器的东西都不是必需的。除非有一些未说明的需求,否则这就是OP所需要的。所有那些任务/多线程/计时器的东西根本不需要。