0

实施后DispatcherTimer,我的涉及用户交互的应用程序不起作用。有关更多信息,我正在尝试使用跳跃运动开发应用程序。需要定时器与 UI 一起工作,以便用户做出一些手势并在定时器结束后停止,但现在跳跃运动在使用调度程序定时器后不再检测任何手势。请给点建议谢谢!

我的计时器代码:

public MainWindow()
{
    dispatcherTimer = new DispatcherTimer();
    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    dispatcherTimer.Tick += dispatcherTimer_Tick;
    dispatcherTimer.Start();
}

void dispatcherTimer_Tick(object sender, EventArgs e)
{
    if (time > 0)
    {
        if (time <= 10)
        {
            if (time % 2 == 0)
            {
                TBCountdown.Foreground = Brushes.Red;
            }
            else
            {
                TBCountdown.Foreground = Brushes.Red;
            }
            time--;
            TBCountdown.Text = string.Format("00:0{0}:0{1}", time / 60, time % 60);
        }
        else
        {
            time--;
            TBCountdown.Text = string.Format("00:0{0}:{1}", time / 60, time % 60);
        }
    }
    else
    {
        dispatcherTimer.Stop();
        MessageBox.Show("Completed");
    }
}
4

2 回答 2

0

感谢所有的建议。Dan Puzey 是对的,它旨在以响应方式与 WPF UI 一起使用。有时它只会滞后一点,但总的来说它仍然应该响应。希望它不再挂起。谢谢!

于 2013-12-16T01:24:56.957 回答
-2

我相信 Dispatacher 正在同一个 UI 线程上运行,并且 hecne 使 UI 无响应。更好的方法是使用 BackgroundWorker 在后台线程上执行耗时的任务。

于 2013-12-13T05:57:12.247 回答