0
我有一個應用程序,它使用DispatcherTimer來管理時間,倒計時等事情,我確實有多個計數器在應用程序打開時打開。但時間有點延遲,我會說大約每分鐘3-5秒。這是我使用的代碼的一部分:在DispatcherTimer中延遲倒計時
DispatcherTimer ob = new DispatcherTimer();
ob.Interval = new TimeSpan(0, 0, 1);
private void bob_Click(object sender, RoutedEventArgs e) //Button starting countdown
{
ob.Start();
tikOb = 140;
ob.Tick += new EventHandler(ob_Tick);
}
void ob_Tick(object sender, EventArgs e)
{
tob.Text = tikOb.ToString();
if (tikOb > 0)
{
tikOb--;
}
else
{
ob.Stop();
tob.Text = "STOPPED";
ob.Tick -= new EventHandler(ob_Tick);
}
//Between these there is a code which is irrelevant in this case.
private void stopob_Click(object sender, RoutedEventArgs e) //Button breaking countdown
{
ob.Tick -= new EventHandler(ob_Tick);
ob.Stop();
tob.Text = "ON";
{
有誰能告訴我爲什麼會發生這種情況?我在代碼中做了什麼錯誤嗎?哦,我還有另外一個使用不同變量的代碼,它是完全分開的。提前致謝!