rev*_*kpi 1 c# silverlight silverlight-3.0 silverlight-4.0
我有两种方法,例如Method1和Method2.在Method1完成后如何调用Method2 500ms?
public void Method1()
{
}
public void Method2()
{
}
Run Code Online (Sandbox Code Playgroud)
使用Timer或BackgroundWorker.除非你想在UI线程上做一些事情,否则Timer可能最适合你的简短描述,在这种情况下,DispatchTimer在你回调UI线程时更适合你.
public void Run_Method1_Then_Method2_500_Milliseconds_Later()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += (s, e) =>
{
// do some quick work here in Method2
Method2(timer);
};
Method1(); // Call Method1 and wait for completion
timer.Start(); // Start Method2 500 milliseconds later
}
public void Method1()
{
// Do some work here
}
public void Method2(DispatcherTimer timer)
{
// Stop additional timer events
timer.Stop();
// Now do some work here
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3637 次 |
最近记录: |