Windows 앱
콘텐츠의 테이블 축소
콘텐츠의 테이블 확장
이 문서는 기계 번역을 이용하여 번역되었습니다. 문서를 영문으로 보려면 영문 확인란을 선택하세요. 마우스 포인터를 텍스트 위로 이동시켜 팝업 창에서 영문 텍스트를 표시할 수도 있습니다.
요청한 주제가 아래에 표시됩니다. 그러나 이 주제는 이 라이브러리에 포함되지 않습니다.

EventHandler<TEventArgs> 대리자


게시 날짜: 2016년 11월


이벤트에서 데이터를 제공하는 경우 이벤트를 처리할 메서드를 나타냅니다.

네임스페이스:   System
어셈블리:  mscorlib(mscorlib.dll에 있음)

public delegate void EventHandler<TEventArgs>(
	object sender,
	TEventArgs e

매개 변수

Type: System.Object

이벤트 소스입니다.

Type: TEventArgs

이벤트 데이터를 포함하는 개체입니다.

형식 매개 변수


이벤트에서 생성되는 이벤트 데이터의 형식입니다.

The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A delegate that refers to a method that provides the response to the event.

  • Optionally, a class that holds the event data, if the event provides data.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

The standard signature of an event handler delegate defines a method that does not return a value. This method's first parameter is of type T:System.Object and refers to the instance that raises the event. Its second parameter is derived from type T:System.EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply the value of the F:System.EventArgs.Empty field. Otherwise, the second parameter is a type derived from T:System.EventArgs and supplies any fields or properties needed to hold the event data.

The T:System.EventHandler`1 delegate is a predefined delegate that represents an event handler method for an event that generates data. The advantage of using T:System.EventHandler`1 is that you do not need to code your own custom delegate if your event generates event data. You simply provide the type of the event data object as the generic parameter.

To associate the event with the method that will handle the event, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

For more information about event handler delegates, see Handling and Raising Events.

The following example shows an event named ThresholdReached. The event is associated with an T:System.EventHandler`1 delegate.

using System;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            Counter c = new Counter(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;

            Console.WriteLine("press 'a' key to increase total");
            while (Console.ReadKey(true).KeyChar == 'a')
                Console.WriteLine("adding one");

        static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);

    class Counter
        private int threshold;
        private int total;

        public Counter(int passedThreshold)
            threshold = passedThreshold;

        public void Add(int x)
            total += x;
            if (total >= threshold)
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.TimeReached = DateTime.Now;

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
            EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
            if (handler != null)
                handler(this, e);

        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;

    public class ThresholdReachedEventArgs : EventArgs
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }

유니버설 Windows 플랫폼
8 이후 사용 가능
.NET Framework
2.0 이후 사용 가능
이식 가능한 클래스 라이브러리
이식 가능한 .NET 플랫폼 에서 지원됨
2.0 이후 사용 가능
Windows Phone Silverlight
7.0 이후 사용 가능
Windows Phone
8.1 이후 사용 가능
맨 위로 이동
© 2017 Microsoft