c# - 有没有办法唤醒 sleep 线程?

标签 c# multithreading sleep

有没有办法在 C# 中唤醒休眠线程?那么,让它休眠很长时间,然后在您需要处理工作时唤醒它?

最佳答案

AutoResetEvent对象(或另一个 WaitHandle 实现)可用于休眠,直到收到来自另一个线程的信号:

// launch a calculation thread
var waitHandle = new AutoResetEvent(false);
int result;
var calculationThread = new Thread(
    delegate
    {
        // this code will run on the calculation thread
        result = FactorSomeLargeNumber();
        waitHandle.Set();
    });
calculationThread.Start();

// now that the other thread is launched, we can do something else.
DoOtherStuff();

// we've run out of other stuff to do, so sleep until calculation thread finishes
waitHandle.WaitOne();

关于c# - 有没有办法唤醒 sleep 线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3460280/

相关文章:

c# - 如何在自动映射器的可查询映射中为枚举创建映射?

c# - 是否有用于简单程序集初始化的良好模式?

c# - asp.net mvc 6 模型绑定(bind)到复杂集合 - IList<T>

Java 线程池/执行器服务和 wait()s - 线程和任务队列发生了什么?

Java Swing 马模拟

NSTimer 在 sleep 模式下继续

c# - Global.asax.cs 中的 Session_End 未使用表单例份验证触发

python - 在 Python 线程化过程中更新 Tkinter 标签

windows - 如何让电脑从待机模式返回

c++ - pthread休眠函数,cpu消耗