WinForm在时间到期后自动关闭?

Sno*_*owy 6 winforms

在Framework 4.0中,我有一个从另一个窗体打开的WinForm,显示一些东西和一个进度条,然后坐在那里.如果用户没有手动关闭它,我想在n secods之后关闭那个"弹出"窗体.最聪明的方法是什么?

谢谢.

Pet*_*lly 11

以所需间隔启动计时器,然后在第一次打勾时关闭表单.像这样的东西

private Timer _timer;

public PopupForm()
{
   InitializeComponent();
   _timer = new Timer();
   _timer.Interval = 5000; // interval in milliseconds here.
   _timer.Tick += (s, e) => this.Close();
   _timer.Start();
}
Run Code Online (Sandbox Code Playgroud)

实际上,最聪明的方法可能是将它放在自己的StartCountdown()方法中,该方法将时间作为参数.像这样的逻辑通常不应该在构造函数中严格地说......