本文介绍了有没有一种方法来唤醒一个沉睡的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来唤醒在C#中的休眠线程?因此,有它睡要么很长一段时间,并唤醒它,当你想工作处理?

Is there a way to wake a sleeping thread in C#? So, have it sleep for either a long time and wake it when you want work processed?

推荐答案

这是的对象(或另一个实现)可以用来睡觉,直到接收到来自另一个线程的信号:

An AutoResetEvent object (or another WaitHandle implementation) can be used to sleep until a signal from another thread is received:

// 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();

这篇关于有没有一种方法来唤醒一个沉睡的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:46