2013-12-13 46 views
0

執行DispatcherTimer後,我的涉及用戶交互的應用程序無法工作。有關更多信息,我正在嘗試使用跳躍運動來開發應用程序。需要計時器與用戶界面一起工作,以便用戶做出一些手勢並在計時器結束後停止,但現在閏秒動作在使用分派器計時器後不再檢測到任何手勢。有些建議請謝謝!UI在DispatcherTimer後無法工作

我的計時器代碼:

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"); 
    } 
} 
+1

當應用程序無響應時,您應該能夠在Visual Studio中暫停它,並查看哪些代碼正在執行。你能編輯你的問題來包含這些信息嗎?我發現你發佈的內容沒有任何明顯的錯誤。 (儘管如此,您只需要初始化'dispatcherTimer')。 –

回答

-2

我beleive的Dispatacher是一樣的UI線程上運行的A和hecne使UI響應。更好的方法是使用BackgroundWorker在後臺線程上執行耗時的任務。

+0

Dispatcher和DispatcherTimer類設計用於以響應的方式使用WPF UI。這裏沒有耗時的任務在前臺線程上運行。 –

0

感謝您的所有建議。 Dan Puzey是對的,它被設計爲以響應的方式與WPF UI一起工作。 有時候它只落後於一個整體,但總體上它仍然會作出迴應。 希望它不會掛起了。謝謝!