【发布时间】:2010-09-28 08:14:35
【问题描述】:
我有一个方法应该延迟运行一段指定的时间。
我应该使用
Thread thread = new Thread(() => {
Thread.Sleep(millisecond);
action();
});
thread.IsBackground = true;
thread.Start();
或者
Timer timer = new Timer(o => action(), null, millisecond, -1);
我读过一些关于使用Thread.Sleep 的articles 是糟糕的设计。但我真的不明白为什么。
但是对于使用 Timer,Timer 有 dispose 方法。由于执行延迟,我不知道如何处置 Timer。你有什么建议吗?
或者,如果您有延迟执行的替代代码,也很感激。
【问题讨论】:
标签: c# multithreading timer sleep delayed-execution