C# DispatcherTimer Start之后立即執行


如果DispatherTimer 的Interval 不是在實例化時賦值,那么Start之后,Tick方法會立即執行一次。

DispatcherTimer timer = new DispatcherTimer(){IsEnabled = true};
timer.Tick += (sender, args) =>
{
Console.WriteLine("timer Tick。");
};
timer.Interval = TimeSpan.FromMilliseconds(3000);
timer.Start();
Console.WriteLine("timer 開始。");

應該這樣初始化:

DispatcherTimer timer = new DispatcherTimer(){Interval = TimeSpan.FromMilliseconds(3000),IsEnabled =  true};
            timer.Tick += (sender, args) =>
            {
                Console.WriteLine("timer Tick。");
            };
            timer.Start();
            Console.WriteLine("timer 開始。");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM