C#Timer在X秒后关闭控制台窗口

Che*_*ire 1 c# console timer

newb在这里.

我试图了解如何编写一个计时器来关闭我的c#控制台应用程序,这样我就不必再次点击'return'来关闭窗口(我一直在使用Console.Read();保持它打开所以我可以看到应用程序按目的运行到目前为止)所以我正在尝试制作一个小计时器方法,下面是我发现的远,但它不运行,因为我不明白什么/如何处理它我想.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lec044b_ForLoop_Sum
{
class Program
{
    public void Play()
    {
        Console.WriteLine("Announce Program");






        Console.WriteLine("Close Program Announcement");

        timer1_Tick(3000);


    }


    private void timer1_Tick(object sender, EventArgs e)
    {
        this.Close();
    }



    static void Main(string[] args)
    {
        Program myProgram = new Program();
        myProgram.Play();

    }
}
Run Code Online (Sandbox Code Playgroud)

}

我已经研究了一些,我已经去了微软资源,这些资源是为那些了解这些东西和新手的人写的,并且看了几个博客并且变得越来越困惑.希望得到一些帮助,这是值得赞赏的.

总结一下 - 我只想让我的控制台窗口在5秒后自动关闭.这就是我想用一个简单的方法做的事情.最终我会尝试将其作为一个类或类似的东西,但我需要小步骤,所以我理解.

干杯

Jim*_*Jim 6

Thread.Sleep会做你想要的:

static void Main(string[] args)
{
    Play();
}

static void Play()
{
    Console.WriteLine("Announce Program");
    Console.WriteLine("Close Program Announcement");
    Thread.Sleep(5000);            
}
Run Code Online (Sandbox Code Playgroud)