WinForms multi-threaded databinding scenario, best practice?

后端 未结 8 1464
渐次进展
渐次进展 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 10:13

    There is an MSDN article specific on that topic. But be prepared to look at VB.NET. ;)

    Additionally maybe you could use System.ComponentModel.BackgroundWorker, instead of a generic second thread, since it nicely formalize the kind of interaction with the spawned background thread you are describing. The example given in the MSDN library is pretty decent, so go look at it for a hint on how to use it.

    Edit: Please note: No marshalling is required if you use the ProgressChanged event to communicate back to the UI thread. The background thread calls ReportProgress whenever it has the need to communicate with the UI. Since it is possible to attach any object to that event there is no reason to do manual marshalling. The progress is communicated via another async operation - so there is no need to worry about neither how fast the UI can handle the progress events nor if the background thread gets interruped by waiting for the event to finish.

    If you prove that the background thread is raising the progress changed event way too fast then you might want to look at Pull vs. Push models for UI updates an excellent article by Ayende.

    0 讨论(0)
  • 2020-12-25 10:13

    This post is old but I thought I'd give options to others. It seems once you start doing async programming and Windows Forms databinding you end up with problems updating Bindingsource datasource or updating lists bound to windows forms control. I am going to try using Jeffrey Richters AsyncEnumerator class from his powerthreading tools on wintellect.

    Reason: 1. His AsyncEnumerator class automatically marshals background threads to UI threads so you can update controls as you would doing Synchronous code. 2. AsyncEnumerator simplifies Async programming. It does this automatically, so you write your code in a Synchronous fashion, but the code is still running in an asynchronous fashion.

    Jeffrey Richter has a video on Channel 9 MSDN, that explains AsyncEnumerator.

    Wish me luck.

    -R

    0 讨论(0)
提交回复
热议问题