Programing

변수 값이 변경 될 때 이벤트를 트리거하는 방법은 무엇입니까?

lottogame 2020. 9. 4. 08:04
반응형

변수 값이 변경 될 때 이벤트를 트리거하는 방법은 무엇입니까?


현재 Visual Studio를 사용하여 C #으로 응용 프로그램을 만들고 있습니다. 변수의 값이 1 일 때 특정 코드가 수행되도록 코드를 만들고 싶습니다. if 문을 사용할 수 있지만 문제는 비동기 프로세스에서 값이 변경되므로 값이 변경되기 전에 기술적으로 if 문을 무시할 수 있다는 것입니다.

변수 값이 변경 될 때 이벤트가 트리거되도록 이벤트 핸들러를 만들 수 있습니까? 그렇다면 어떻게해야합니까?

if 문이 어떻게 작동하는지 오해했을 가능성이 있습니다! 어떤 도움이라도 대단히 감사하겠습니다.


당신이 속성을 만들고 싶어하는 것 같습니다.

public int MyProperty
{
    get { return _myProperty; }
    set
    {
        _myProperty = value;
        if (_myProperty == 1)
        {
            // DO SOMETHING HERE
        }
    }
}

private int _myProperty;

이렇게하면 속성 값이 변경 될 때마다 일부 코드를 실행할 수 있습니다. 원하는 경우 여기에서 이벤트를 제기 할 수 있습니다.


필드 값이 변경 될 때마다 속성 설정기를 사용하여 이벤트를 발생시킬 수 있습니다.

고유 한 EventHandler 대리자를 가질 수도 있고 유명한 System.EventHandler 대리자를 사용할 수도 있습니다.

일반적으로 이에 대한 패턴이 있습니다.

  1. EventArgs 유형의 인수가있는 이벤트 처리기 대리자를 사용하여 공용 이벤트를 정의합니다.
  2. OnXXXXX (예 : OnMyPropertyValueChanged)라는 보호 된 가상 메서드를 정의합니다. 이 메서드에서 이벤트 처리기 대리자가 null인지, 그렇지 않은 경우이를 호출 할 수 있는지 확인해야합니다 (이벤트 위임에 연결된 메서드가 하나 이상 있음을 의미 함).
  3. 구독자에게 변경 사항을 알리고 싶을 때마다이 보호 된 메서드를 호출하십시오.

여기에 예가 있습니다.

private int _age;

//#1
public event System.EventHandler AgeChanged;

//#2
protected virtual void OnAgeChanged()
{ 
     if (AgeChanged != null) AgeChanged(this,EventArgs.Empty); 
}

public int Age
{
    get
    {
         return _age;
    }

    set
    {
         //#3
         _age=value;
         OnAgeChanged();
    }
 }

이 접근 방식의 장점은 클래스에서 상속하려는 다른 클래스가 필요한 경우 동작을 변경하도록 허용한다는 것입니다.

If you want to catch an event in a different thread that it's being raised you must be careful not to change the state of objects that are defined in another thread which will cause a cross thread exception to be thrown. To avoid this you can either use an Invoke method on the object that you want to change its state to make sure that the change is happening in the same thread that the event has been raised or in case that you are dealing with a Windows Form you can use a BackgourndWorker to do things in a parallel thread nice and easy.


The .NET framework actually provides an interface that you can use for notifying subscribers when a property has changed: System.ComponentModel.INotifyPropertyChanged. This interface has one event PropertyChanged. Its usually used in WPF for binding but I have found it useful in business layers as a way to standardize property change notification.

In terms of thread safety I would put a lock under in the setter so that you don't run into any race conditions.

Here are my thoughts in code :) :

public class MyClass : INotifyPropertyChanged
{
    private object _lock;

    public int MyProperty
    {
        get
        {
            return _myProperty;
        }
        set
        {
            lock(_lock)
            {
                //The property changed event will get fired whenever
                //the value changes. The subscriber will do work if the value is
                //1. This way you can keep your business logic outside of the setter
                if(value != _myProperty)
                {
                    _myProperty = value;
                    NotifyPropertyChanged("MyProperty");
                }
            }
        }
    }

    private NotifyPropertyChanged(string propertyName)
    {
        //Raise PropertyChanged event
    }
    public event PropertyChangedEventHandler PropertyChanged;
}


public class MySubscriber
{
    private MyClass _myClass;        

    void PropertyChangedInMyClass(object sender, PropertyChangedEventArgs e)
    {
        switch(e.PropertyName)
        {
            case "MyProperty":
                DoWorkOnMyProperty(_myClass.MyProperty);
                break;
        }
    }

    void DoWorkOnMyProperty(int newValue)
    {
        if(newValue == 1)
        {
             //DO WORK HERE
        }
    }
}

Hope this is helpful :)


just use a property

int  _theVariable;
public int TheVariable{
  get{return _theVariable;}
  set{
    _theVariable = value; 
    if ( _theVariable == 1){
      //Do stuff here.
    }
  }
}

참고URL : https://stackoverflow.com/questions/5842339/how-to-trigger-event-when-a-variables-value-is-changed

반응형