Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

c# - Limit binding updates per second

I'm currently creating a program that reads out data sent via a COM port and then plots it live in a diagram. The data is displayed using the MVVM principle, which works fine when data is sent at around 10Hz. However, the device the data is being read from can go up to a refresh rate of 1 kHz, which means 1000 datasets per minute. This works fine for displaying and updating simple textboxes, however it breaks the diagram because the updating is happening too fast.

What I think I need to do now is limit the amount of update events that is sent to the subscribed classes and pages, so that only a limited amount of data is sent through, which gives the diagram a chance to draw properly. Is there a way to limit this automatically, or what code adjustments would you suggest to do just that manually?

A small code snippet from my collection changed event:

void dataItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    NotifyPropertyChanged("dataItems");
    NotifyPropertyChanged("lastItem");

    // update any charts
    NotifyPropertyChanged("AccelXData");
    NotifyPropertyChanged("AccelYData");
    NotifyPropertyChanged("AccelZData");
}

// handle property changes
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
    var handler = this.PropertyChanged;
    if (handler != null)
        handler(this, new PropertyChangedEventArgs(propertyName));
}

Every dataset also has an ID that maybe can be used to check when to update manually, as an idea.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A better approach would to remove the calls to NotifyPropertyChanged whenever the data changes.

Create a timer and refresh on the timer. That way you can control the refresh rate, and it is not bound to the rate at which the data arrives.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...