Winforms UI Thread Marshalling BusinessObject List Updated from Background Thread

Firstly, I know that there are many questions and solutions to fix the sorting of threads from threads other than background threads. All the questions and solutions that I found focused on scenarios in which the list or business object itself triggers an event that Windows Form can subscribe to, and then correctly translate the update into the main user interface thread.

In my case, the list of business objects is updated by the background thread in a separate layer. I want this list to bind to a control in the main thread. Do I really need to expose the event in the user interface from the list of business objects so that the update can be configured correctly ?. Can I calmly update the list of business objects and distribute these updates in the user interface without causing the display of the changed list event in the user interface?

EDIT:

My problem is basically this: Modified INotifyProperty after the property has been changed. The control associated with the object that implements this interface will try to update, if the thread causing the event is not a UI thread, we have a problem. Therefore, we need to notify the user interface thread that we want to update so that the update can be handled in a thread-safe manner, which means that the background stream update objects cannot just do their business, it must ask permission to update the objects or ask the user interface changes to the object on its behalf. This is what I mean when the user interface gets pulled into handle object updates.

+3
2

( ) usenet - ThreadedBindingList ( - , ); , IMO ...

( )

+1

, -, , , , , .

, UI, , .

., (, INotifyPropertyChanged, ), , , , , , , UI, .

:. , , - - "STFU", ​​ true, , UI. "OnRaiseMyEvent (...)" STFU - true, STFU, , .

# 2:. , , : ISynchronizeInvoke -. - , - :

public class MyObject { 
   private ISynchronizeInvoke _Invoker;

   public MyObject(ISynchronizeInvoke invoker) { 
      _Invoker = invoker;
   }

   private void OnPropertyChanged(string propertyName) {
      PropertyChangedEventHandler handlers = this.PropertyChanged;
      if (handlers != null) { 
      if (_Invoker.InvokeRequired) { 
        _Invoker.Invoke(handlers, new PropertyChangedEventArgs(propertyName)); 
      } else { 
         handlers(new PropertyChangedEventArgs(propertyName);
      }
   }
}
0

Source: https://habr.com/ru/post/1711765/


All Articles