블로그 이미지
따시쿵

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

'ManualResetEvent 클래스'에 해당되는 글 1

  1. 2014.12.24 ManualResetEvent 클래스
2014. 12. 24. 11:42 C#

하나 이상의 대기 중인 스레드에 이벤트가 발생했음을 알립니다. 이 클래스는 상속될 수 없습니다.


ManualResetEvent 를 사용하여 스레드는 신호를 보냄으로써 서로 통신할 수 있습니다. 일반적으로 이 통신은 다른 스레드가 처리되기 위해 먼저 하나의 스레드가 완료해야 하는 작업과 관련이 있습니다.


하나의 스레드에서 다른 스레드의 처리를 위해 먼저 완료되어야 하는 작업을 시작할 때 Reset을 호출하여 ManualResetEvent를 신호 없음 상태로 설정합니다. 이 스레드는 ManualResetEvent를 제어하는 것으로 간주될 수 있습니다. ManualResetEvent 에 대해 WaitOne을 호출하는 스레드는 차단되며 신호를 기다립니다. 제어 스레드는 해당 작업을 끝나면 Set을 호출하여 대기 중인 스레드가 계속될 수 있음을 알립니다. 대기 중인 모든 스레드는 해제됩니다.


출처 : http://msdn.microsoft.com/ko-kr/library/system.threading.manualresetevent(v=vs.110).aspx



실행 화면



예제 프로그램

       // mre is used to block and relrease threads manually. It is 
        // created in unsignaled state.
        private static ManualResetEvent mre = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent\n");

            for (int i = 0; i <= 2; i++)
            {
                Thread t = new Thread(ThreadProc);
                t.Name = "Thread_" + i.ToString();
                t.Start();
            }

            #region 테스트1 - Set()
            Thread.Sleep(500);
            Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()\n" +
                              "\nto release all the threads.\n");
            Console.ReadLine();

            mre.Set();

            #endregion

            #region 테스트2 - Reset()
            Thread.Sleep(500);
            Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
                              "\ndo not block. Press Enter to show this.\n");
            Console.ReadLine();

            for (int i = 3; i <= 4; i++)
            {
                Thread t = new Thread(ThreadProc);
                t.Name = "Thread_" + i.ToString();
                t.Start();

            }
            Thread.Sleep(500);
            Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block\n" +
                              "\nwhen they call WaitOne\n");
            Console.ReadLine();

            mre.Reset();

            #endregion

            #region 테스트3 - Set()
            //Start a thread that waits on the ManualResetEvent
            Thread t5 = new Thread(ThreadProc);
            t5.Name = "Thread_5";
            t5.Start();

            Thread.Sleep(500);
            Console.WriteLine("\nPress Enter to call Set() and conclude the demo\n");
            Console.ReadLine();

            mre.Set();

            #endregion

            Console.ReadLine();
        }

        private static void ThreadProc()
        {
            string name = Thread.CurrentThread.Name;

            Console.WriteLine(name + " starts and call mre.WaitOne()");

            mre.WaitOne();

            Console.WriteLine(name + " ends");
        }


posted by 따시쿵
prev 1 next