다음을 통해 공유


방법: 클래스에 이벤트 구현

업데이트: 2007년 11월

다음 절차에서는 클래스의 이벤트를 구현하는 방법을 설명합니다. 첫 번째 절차에서는 연결된 데이터가 없는 이벤트를 구현합니다. 여기서는 이벤트 데이터와 대리자 처리기의 System.EventArgsSystem.EventHandler 클래스를 사용합니다. 두 번째 절차에서는 사용자 지정 데이터가 있는 이벤트를 구현합니다. 여기서는 이벤트 데이터와 이벤트 대리자 처리기의 사용자 지정 클래스를 정의합니다.

이벤트를 발생시키고 처리하는 방법을 보여 주는 전체 샘플은 방법: 이벤트 발생 및 사용을 참조하십시오.

이벤트 관련 데이터 없이 이벤트를 구현하려면

  1. 클래스에서 공용 이벤트 멤버를 정의합니다. 이벤트 멤버의 형식을 System.EventHandler 대리자로 설정합니다.

    public class Countdown 
    {
        ...
        public event EventHandler CountdownCompleted;   
    }
    
    Public Class Countdown
        ...
        Public Event CountdownCompleted As EventHandler
    End Class
    
  2. 이벤트를 발생시키는 보호된 메서드를 클래스에 제공합니다. 메서드 이름을 OnEventName으로 지정합니다. 메서드 내에서 이벤트를 발생시킵니다.

    public class Countdown 
    {
        ...
        public event EventHandler CountdownCompleted;   
        protected virtual void OnCountdownCompleted(EventArgs e)    {        if (CountdownCompleted != null)            CountdownCompleted(this, e);    }
    }
    
    Public Class Countdown
       ...
       Public Event CountdownCompleted As EventHandler
       Protected Overridable Sub OnCountdownCompleted(e As EventArgs)      RaiseEvent CountdownCompleted(Me, e)   End Sub
    End Class
    
  3. 클래스에서 이벤트를 발생시킬 시기를 결정합니다. OnEventName을 호출하여 이벤트를 발생시킵니다.

    public class Countdown 
    {
        ...
        public void Decrement
        {
            internalCounter = internalCounter - 1;
            if (internalCounter == 0)
                OnCountdownCompleted(new EventArgs());
        }
    }
    
    Public Class Countdown
        ...
        Public Function Decrement
            InternalCounter = internalCounter - 1
            If internalCounter = 0
                OnCountdownComplete(New EventArgs())
            End If
        End Function
    End Class
    

이벤트 관련 데이터가 있는 이벤트를 구현하려면

  1. 이벤트에 대한 데이터를 제공하는 클래스를 정의합니다. 클래스 이름을 EventNameArgs로 지정하고 System.EventArgs에서 클래스를 파생한 다음 이벤트 관련 멤버를 추가합니다.

    public class AlarmEventArgs : EventArgs 
    {
       private readonly int nrings = 0;
       private readonly bool snoozePressed = false;
    
       //Constructor.
       public AlarmEventArgs(bool snoozePressed, int nrings) 
       {
          this.snoozePressed = snoozePressed;
          this.nrings = nrings;
       }
    
       //Properties.
       public string AlarmText {  
          ...
       }
       public int NumRings {
          ...
       }
       public bool SnoozePressed{
          ...
       }
    }
    
    Public Class AlarmEventArgs
       Inherits EventArgs
       Private nrings As Integer = 0
       Private _snoozePressed As Boolean = False
    
       'Constructor.
       Public Sub New(ByVal snoozePressed As Boolean, ByVal nrings As Integer) 
          Me.snoozePressed = snoozePressed
          Me.nrings = nrings
       End Sub
    
       'Properties.
       Public ReadOnly Property AlarmText() As String
          ...
       End Property 
    
       Public ReadOnly Property NumRings() As Integer
          ...
       End Property 
    
       Public ReadOnly Property SnoozePressed() As Boolean
          ...
       End Property
    End Class
    
  2. 이벤트의 대리자를 선언합니다. 대리자 이름을 EventNameEventHandler로 지정합니다.

    public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
    
    Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)
    
  3. 클래스에서 EventName이라는 공용 이벤트 멤버를 정의합니다. 이벤트 멤버의 형식을 이벤트 대리자 형식으로 설정합니다.

    public class AlarmClock 
    {
        ...
        public event AlarmEventHandler Alarm;
    }
    
    Public Class AlarmClock
        ...
        Public Event Alarm As AlarmEventHandler
    End Class
    
  4. 이벤트를 발생시키는 보호된 메서드를 클래스에서 정의합니다. 메서드 이름을 OnEventName으로 지정합니다. 메서드 내에서 이벤트를 발생시킵니다.

    public class AlarmClock 
    {
        ...
        public event AlarmHandler Alarm;
        protected virtual void OnAlarm(AlarmEventArgs e)    {      if (Alarm != null)           Alarm(this, e);     }
    }
    
    Public Class AlarmClock
        ...
        Public Event Alarm As AlarmEventHandler
        Protected Overridable Sub OnAlarm(e As AlarmEventArgs)        RaiseEvent Alarm(Me, e)    End Sub
    End Class
    
  5. 클래스에서 이벤트를 발생시킬 시기를 결정합니다. OnEventName을 호출하여 이벤트를 발생시키고 EventNameEventArgs를 사용하여 이벤트 관련 데이터를 전달합니다.

    Public Class AlarmClock
    {
        ...
        public void Start()
        {
            ...
            System.Threading.Thread.Sleep(300);
            AlarmEventArgs e = new AlarmEventArgs(false, 0);
            OnAlarm(e);
        }
    }
    
    Public Class AlarmClock
        ...
        Public Function Start
            ...
            System.Threading.Thread.Sleep(300)
            Dim e As AlarmEventArgs = New AlarmEventArgs(False, 0)
            OnAlarm(e)
        End Function
    End Class
    

참고 항목

작업

방법: 이벤트 발생 및 사용

개념

이벤트 및 대리자

이벤트 발생시키기

기타 리소스

이벤트 처리 및 발생