Forum General

Monitor boolean for changing value to certain value such as False only.

Cdn_EuroCdn_Euro Member ✭✭✭

Hi,

I would like to be able to trigger an event when a boolean changes value to false. I know how to trigger the event when the boolean changes value but any guidance as to how to only trigger event when boolean is false?

Tagged:

Answers

  • Cdn_EuroCdn_Euro Member ✭✭✭

    It was actually very simple. Here is how I did it:

    private bool isMonitoring = false;
    
    private bool IsMonitoring
            {
                get
                {
                    return isMonitoring;
                }
                set
                {
                    if (isMonitoring != value)
                    {
                        isMonitoring = value;
    
                        OnPropertyChanged(nameof(IsMonitoring));
    
                        // trigger action if set to false
                        if (isMonitoring == false)
                        {
                            PrintLogLine("Monitoring bool changed to false.");
                        }
                    }
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
Sign In or Register to comment.