WinForms multi-threaded databinding scenario, best practice?

后端 未结 8 1465
渐次进展
渐次进展 2020-12-25 09:23

I\'m currently designing/reworking the databinding part of an application that makes heavy use of winforms databinding and updates coming from a background thread (once a se

8条回答
  •  北海茫月
    2020-12-25 09:51

    Yes all the books show threaded structures and invokes etc. Which is perfectly correct etc, but it can be a pain to code, and often hard to organise so you can make decent tests for it

    A UI only needs to be refreshed so many times a second, so performance is never an issue, and polling will work fine

    I like to use a object graph that is being continuously updated by a pool of background threads. They check for actual changes in data values and when they notice an actual change they update a version counter on the root of the object graph (or on each main item whatever makes more sense) and updates the values

    Then your foreground process can have a timer (same as UI thread by default) to fire once a second or so and check the version counter, and if it changes, locks it (to stop partial updates) and then refreshes the display

    This simple technique totally isolates the UI thread from the background threads

提交回复
热议问题