当消息框显示时,Dispatchertimer不继续

本文关键字:Dispatchertimer 继续 消息 显示 | 更新日期: 2023-09-27 18:12:01

在我的wp8应用程序中,我有一个使用dispatchertimer的倒计时计时器。我单击一个按钮并显示一个消息框。当显示消息框时,倒计时计时器没有继续。我希望timer_counter在消息框显示的时间内继续倒计时。

我试过System.Timers。定时器,但它找不到类。此外,我尝试了DispatcherPriority使定时器在后台工作。但是它也找不到类。

private DispatcherTimer timer;
private int timer_counter;
public MainPage()
{
    InitializeComponent();
    // set timer
    timer_counter = 30; // 30 secounds
    CountdownTimer.Text = new TimeSpan(0, 0, timer_counter).ToString("mm':'ss"); // show the time to textbox
    timer = new DispatcherTimer();  
    timer.Interval = new TimeSpan(0, 0, 1); // interval: 1 second
    timer.Tick += new EventHandler(dispatcherTimer_Tick);
    timer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    timer_counter--; // countdown
    CountdownTimer.Text = new TimeSpan(0, 0, timer_counter).ToString("mm':'ss"); // show the time to textbox
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBoxResult result = MessageBox.Show(
                     "clickbutton",
                     "alert",
                     MessageBoxButton.OK);
}

当消息框显示时,Dispatchertimer不继续

这就是DispatcherTimer的设计。在您的示例中,计时器tick回调在UI线程中运行。当UI线程正在使用时-它等待…