我需要实现一个非阻塞的MessageBox
,它会在20秒后自动关闭。请问有人能建议我怎样做吗?
使用DispatcherTimer创建一个新的窗口。当窗口打开时,启动计时器。然后您有两个选择:
当你说非阻塞时,我立刻排除了MessageBox类(假设你所说的非阻塞是指非模态对话框?)。
相反,您可以创建一个窗口,该窗口是MessageBox的自己实现。要使其非模态,您调用Show()方法。然后,您只需设置一个20秒的计时器来调用关闭方法:
DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
//Constructor
public MyMessageBox()
{
timer.Interval = TimeSpan.FromSeconds(20d);
timer.Tick += new EventHandler(timer_Tick);
}
public new void Show()
{
base.Show();
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
//set default result if necessary
timer.Stop();
this.Close();
}
以上假定您已创建一个名为MyMessageBox的类,该类继承自Window。