C# WaitHandle.WaitOne

jiyul·2024년 2월 21일
0

C#

목록 보기
21/21

현재 WaitHandle이(가) 신호를 받을 때까지 현재 스레드를 차단합니다.

using System;
using System.Threading;

class WaitOne
{
    static AutoResetEvent autoEvent = new AutoResetEvent(false);

    static void Main()
    {
        Console.WriteLine("Main starting.");

        ThreadPool.QueueUserWorkItem(
            new WaitCallback(WorkMethod), autoEvent);

        // Wait for work method to signal.
        autoEvent.WaitOne();
        Console.WriteLine("Work method signaled.\nMain ending.");
    }

    static void WorkMethod(object stateInfo) 
    {
        Console.WriteLine("Work starting.");

        // Simulate time spent working.
        Thread.Sleep(new Random().Next(100, 2000));

        // Signal that work is finished.
        Console.WriteLine("Work ending.");
        ((AutoResetEvent)stateInfo).Set();
    }
}

autoEvent.Set() 을 실행하기 전까지 대기합니다.

profile
Let's take the lead

0개의 댓글

관련 채용 정보