开发者

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.E开发者_运维技巧lapsed += (sender, e) =>
    {
      cmdClose_Click(null, null); //Attached to a button which normally does the job of closing the window.
    };


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜