4
이 코드에서 AutoResetEvent 및 bool 변수를 사용하여 스레드를 일시 중지/다시 시작하려고합니다. 블로킹이 == true 일 때마다 매번 테스트를 중단 할 수 있습니까 (For 루프의 Work())? "차단 된"변수의 테스트는 또한 잠금이 필요하며 시간이 많이 소요됩니다.스레드를 일시 중지/다시 시작 AutoResetEvent
class MyClass
{
AutoResetEvent wait_handle = new AutoResetEvent();
bool blocked = false;
void Start()
{
Thread thread = new Thread(Work);
thread.Start();
}
void Pause()
{
blocked = true;
}
void Resume()
{
blocked = false;
wait_handle.Set();
}
private void Work()
{
for(int i = 0; i < 1000000; i++)
{
if(blocked)
wait_handle.WaitOne();
Console.WriteLine(i);
}
}
}