Silverlight run DispatchTimer only once

痴心易碎 提交于 2019-12-25 08:58:06

问题


I'm trying to figure out a way in Silverlight / C# to make a DispatchTimer run only once.

I have a user form and when submitted I want to display a message for 10 seconds and then disappear and kill the DispatchTimer thread.

I know how to make a DispatchTimer that repeats:

clock.Interval = TimeSpan.FromSeconds(10);
clock.Tick += clockTick;

clock.Start();

But I want that thread to end as soon as it completes.


回答1:


This should work for you:

DispatcherTimer clock = new DispatcherTimer();
clock.Interval = TimeSpan.FromSeconds(10);
clock.Tick += (object sender, EventArgs e) =>
{
    clock.Stop();
    // Some code here
};
clock.Start();

An anonymous event handler will also keep things "in the same place" in case you don't want to widen the scope of your DispatcherTimer object.




回答2:


Stop the timer in your clockTick handler once it fires.



来源:https://stackoverflow.com/questions/11438631/silverlight-run-dispatchtimer-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!