2010-03-09 38 views
2

我正在開發具有自定義動畫的Silverlight應用程序。我想每1毫秒更新一次變量animationCounter,以便在一秒鐘內的值爲1000.我試過DispatcherTimer和System.Threading.Timer。這種方式:Silverlight計時器問題

DispatcherTimer timer = new DispatcherTimer(); (...) 
timer.Interval = new TimeSpan(0, 0, 0, 0, 1); 
timer.Tick += new EventHandler(timer_Tick); (...) 

(...)

void timer_Tick(object sender, EventArgs e) 
{ 
     animationCounter++; 
     Dispatcher.BeginInvoke(() => txtAnimationCounter.Text = animationCounter.ToString()); 
} 

與System.Threading.Timer

System.Threading timer = null; 
timer = new System.Threading.Timer(UpdateAnimationCounter, 0, 1); 

void UpdateAnimationCounter(object state) 
{ 
       animationCounter++; 
     Dispatcher.BeginInvoke(() => txtAnimationCounter.Text = animationCounter.ToString()); 
} 

他們兩個都設置約100 AnimationCounter在一秒鐘。應該是1000.我不知道爲什麼。有什麼我失蹤了嗎?

感謝

回答

3

文件應說明計時器不具備的1ms的分辨率,但最少10ms的;)它沒有tseem來。無論如何,最小定時器分辨率大約是10毫秒......所以這是他們觸發的最小時間間隔。

爲什麼heck(對不起)你需要1ms嗎?聽起來對我毫無用處。動畫應該可以,每秒大約25到60次更新 - 剩下的眼睛無法看到。

+0

我也沒有發現任何文檔說明10毫秒的分辨率,但其他一些網站暗示。 – 2010-03-09 11:36:36

+0

謝謝。我不知道。是的,我認爲10毫秒的分辨率應該可以。我需要這個值來同步啓動一些動畫事件。 – jose 2010-03-09 11:39:11