WinForm Automatically Close After Time Expires?
In Framework 4.0, I have a WinForm that is opened from another form, displays some stuff and a progress ba开发者_StackOverflow中文版r, and then sits there. I would like to close that "pop up" form after n secods if the user does not close it manually. What's the smartest way to do that?
Thanks.
Start a timer with the desired interval and then when it ticks the first time, close the form. Something like this
private Timer _timer;
public PopupForm()
{
InitializeComponent();
_timer = new Timer();
_timer.Interval = 5000; // interval in milliseconds here.
_timer.Tick += (s, e) => this.Close();
_timer.Start();
}
Actually the smartest way would probably putting this in its own StartCountdown() method that takes the time as a parameter. Logic like this normally shouldn't be in a constructor strictly speaking...
精彩评论