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 开始。");

 

转载于:https://www.cnblogs.com/akiing/p/7591827.html

你可能感兴趣的:(C# DispatcherTimer Start之后立即执行)