相关疑难解决方法(0)

使用Thread.Sleep和Timer进行比较以延迟执行

我有一个方法应该延迟运行指定的时间.

我应该用吗?

Thread thread = new Thread(() => {
    Thread.Sleep(millisecond);
    action();
});
thread.IsBackground = true;
thread.Start();
Run Code Online (Sandbox Code Playgroud)

要么

Timer timer = new Timer(o => action(), null, millisecond, -1);
Run Code Online (Sandbox Code Playgroud)

我读过一些关于使用的文章Thread.Sleep是糟糕的设计.但我真的不明白为什么.

但是对于使用Timer,Timer有配置方法.由于执行延迟,我不知道如何配置Timer.你有什么建议吗?

或者如果你有延迟执行的替代代码也很感激.

c# multithreading sleep timer delayed-execution

56
推荐指数
3
解决办法
6万
查看次数

标签 统计

c# ×1

delayed-execution ×1

multithreading ×1

sleep ×1

timer ×1