Robin Maben
Robin Maben

Reputation: 23064

Execute a default action for MessageBox after an interval has elapsed

Let's say I have a custom message box used as follows

MyWindow.ShowDialog();

I need to Close(), Hide() or execute a handler say Close_Click after say 1 second if no user action occurs.

Thread.Sleep() and Timer approaches didn't help much.

EDIT:

I did this in the window constructor

    var timer = new System.Timers.Timer(timeOut);
    timer.Start();
    timer.Elapsed += (sender, e) =>
    {
      cmdClose_Click(null, null); //Attached to a button which normally does the job of closing the window.
    };

Upvotes: 1

Views: 678

Answers (3)

Gimno
Gimno

Reputation: 6606

If you are using WPF, using a DispatcherTimer (System.Windows.Threading) on your custom message box will work. Just use the Tick event to call Close().

Upvotes: 3

Mario
Mario

Reputation: 36497

One second sounds too short to me, but anyway.

Just add a Timer control to your custom message box form, enable it and set its Intervall to 1000. Then add a event for it's timer event and put the call to Close() inside.

Upvotes: 1

Brent Newbury
Brent Newbury

Reputation: 373

I've had to do something similar in the past. What I did was initialise a System.Threading.Timer that ticks periodically. On each tick if I can close the window (because some background operation has completed) I set the DialogResult and call Close (making sure to check InvokeRequired).

Upvotes: 1

Related Questions