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

后端 未结 4 1550
梦谈多话
梦谈多话 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

    If you use the System.Threading.Timer you can use the DueTime to set the first time it fires as the time you want to close your application

    new System.Threading.Timer((o) => Application.Exit(), null, (Configs.EndService - DateTime.Now), TimeSpan.FromSeconds(0));
    Application.Run(new Form1());
    

提交回复
热议问题