K M*_*hta 6 c# silverlight wpf dispatchertimer
做这样的事情是安全的:
private void MyFunction()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += (object sender, object e) =>
{
timer.Stop();
// Some code here
};
timer.Start();
}
Run Code Online (Sandbox Code Playgroud)
Matt提出了一种观点,即你附加匿名方法的方式是没有简单的方法来分离它.以下是可用于在必要时分离的一般模式.
private void MyFunction()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
EventHandler eh = null;
eh = (object sender, object e) =>
{
timer.Tick -= eh;
timer.Stop();
// Some code here
};
timer.Tick += eh;
timer.Start();
}
Run Code Online (Sandbox Code Playgroud)
但是在这种特定情况下,原始代码的工作方式没有任何问题,因为定时器一旦停止就会变为可收集状态.
| 归档时间: |
|
| 查看次数: |
5023 次 |
| 最近记录: |