Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForm Automatically Close After Time Expires?

Tags:

winforms

In Framework 4.0, I have a WinForm that is opened from another form, displays some stuff and a progress bar, 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.

like image 319
Snowy Avatar asked Sep 22 '11 18:09

Snowy


1 Answers

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...

like image 116
Peter Kelly Avatar answered Oct 17 '22 13:10

Peter Kelly