What is the proper way to programmatically close a WinForms application after a certain time?

后端 未结 4 1427
梦谈多话
梦谈多话 2021-01-19 01:25

I start my form in the usual way:

Application.Run(new MainForm());

I want it to open and run until a certain time, then close. I\'ve tried

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-19 01:35

    How about something like this:

    public partial class Form1 : Form
    {
        private static Timer _timer = new Timer();
    
        public Form1()
        {
            InitializeComponent();
            _timer.Tick += _timer_Tick;
            _timer.Interval = 5000; // 5 seconds
            _timer.Start();            
        }
    
        void _timer_Tick(object sender, EventArgs e)
        {
            // Exit the App here ....
            Application.Exit();
        }
    }
    

提交回复
热议问题