c# - How resource efficient is Data Binding vs BeginInvoke (for the purpose of GUI manipulation) -


i did search , closest thing question have in mind how refresh visual control properties (textblock.text) set inside loop?

the example included in url in situation, except reading in constant changing stream of data, , want changes in values reflected in windows interface.

i trying make program efficient possible, should use (inotifypropertychanged + data binding)

or following better?

application.current.dispatcher.begininvoke(system.windows.threading.dispatcherpriority.background,    new action () => label1.content = some_content)) 

assuming perform lot of buffer/checksum operations using system timers.

wpf, other ui frameworks, requires modify ui controls same thread on created. thread, referred "ui thread", application's main thread, i.e. 1 program execution started.

if happen subscribe serialport.datareceived event, facing problem, because event might triggered on (background) thread:

the datareceived event raised on secondary thread when data received serialport object. because event raised on secondary thread, , not main thread, attempting modify elements in main thread, such ui elements, raise threading exception. if necessary modify elements in main form or control, post change requests using invoke, work on proper thread.

meaning inside event handler, may not directly manipulate ui controls.

that wpf's dispatcher.begininvoke comes in: helps code on ui thread. pass delegate, , delegate invoked on ui thread. therefore allowed manipulate ui inside delegate.

data binding no real alternative begininvoke: data binding manipulates ui, based on changes data object (or vice versa), not automatically switch ui thread. means if you're using data binding, must change data context if you're on ui thread... need begininvoke anyway.

that being said, if use of data binding makes code simpler , easier understand, means use it! might still need dispatcher.begininvoke:

private void serialport_datareceived(object sender, serialdatareceivedeventargs e) {     application.current.dispatcher.begininvoke(..., () => datacontext.someproperty = serialport...; } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -