我有两种方法,例如 Method1 和 Method2。如何在 Method1 完成后 500ms 调用 Method2?
public void Method1()
{
}
public void Method2()
{
}
最佳答案
使用 Timer或 BackgroundWorker . Timer 可能最适合您的简短描述,除非您想在 UI 线程上执行某些操作,在这种情况下 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
}
关于c# - 如何在 SilverLight 中的某个时间(500 毫秒)调用某个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9618342/