简体   繁体   中英

Change data in Class when List<sub-class> data is changed

I have a class STimer that has a List in it. The serviceDetail is monitored on a timer very often, and rarely changes, but when it does change I want to get the fact that the data changed without looping through the list due to processing power. Maybe this is a duplicate question that I just don't know how to search for it, but I have been trying. Here is a code Sample:

class STimers
{

public class ServiceDetail
    {
        private int _serviceKey;
        private bool _isRunning = true;
        private bool _runningStateChanged = false;

        public bool isRunning
        {
            get { return _isRunning; }
            set
            {
                //Check to see if the data is the same, if so, don't change, if not, change and flag as changed
                if(_isRunning = value) { return; }
                else
                {
                    _isRunning = value;
                    _runningStateChanged = true;
        <-- Update STimers._dataChanged to true -->
                }
            }
        }

    }


public List<ServiceDetail> _serviceMonitors = new List<ServiceDetail>();
public bool _dataChanged = false;


}

I could do a .Find on the list to return all of the _serviceMonitors._runningStateChanged=true, but that seems like a lot of work parsing the List every time the timer fires, when likely only 1 out of 1,000 loops will actually have a change.

Is this even possible, or do I need to move the check for changes out of the class?

You could get this by adding an event to your ServiceDetail class

public class ServiceDetail
{
    public event EventHandler<ListChangedEventArgs> ListChanged;
    private int _serviceKey;
    private bool _isRunning = true;
    private bool _runningStateChanged = false;

    private void OnListChanged(ListChangedEventArgs e){
        if (ListChanged != null) ListChanged(this, e);
    }       

    public bool isRunning
    {
        get { return _isRunning; }
        set
        {
            //Check to see if the data is the same, if so, don't change, if not, change and flag as changed
            if(_isRunning = value) { return; }
            else
            {
                _isRunning = value;
                _runningStateChanged = true;
                OnListChanged(new ListChangedEventArgs(this));
    <-- Update STimers._dataChanged to true -->
            }
        }
    }

}

And define your ListChangedEventArgs class like this

public class ListChangedEventArgs:EventArgs
{
    public ServiceDetail serviceDetail { get; set; }
    public ListChangedEventArgs(ServiceDetail s)
    {
        serviceDetail = s;
    }
}

And then register to the event for each servicedetail added to the list

s.ListChanged += (sender, args) => YourFunction();

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM