c# - How can I perform WinForm cross threading operations without a reference to the affected control? -


i implemented few generic binding collections, used datasources controls of winform application.

my problem collections may not accessed same thread 1 of control bounded , lead cross-thread exceptions or maybe other issues. trying marshal thread crossing operations, considering rather not have reference control outside ui thread, not use neither control.invoke method nor begininvoke one.

after deeper investigation, have feeling problem solved accessing either ui thread dispatcher or synchronization context, , use them inside of onlistchanged function of binding collection.

like make call invoke or send methods if necessary.

if using wpf think should possible use application.current.dispatcher application dispatcher @ time.
there alike solution use winform application? or else allow me check within thread list modification performed , marshal cross-threading operations if needed

i found piece of code interesting me, unfortunately in case handler target not dispatcherobject currency manager. there way have same behaviour ?

  protected virtual void oncollectionchanged(notifycollectionchangedeventargs args) { var notifycollectionchangedeventhandler = collectionchanged;  if (notifycollectionchangedeventhandler == null)     return;  foreach (notifycollectionchangedeventhandler handler in notifycollectionchangedeventhandler.getinvocationlist())     {     var dispatcherobject = handler.target dispatcherobject;      if (dispatcherobject != null && !dispatcherobject.checkaccess())         {         dispatcherobject.dispatcher.invoke(dispatcherpriority.databind, handler, this, args);         }     else         handler(this, args); // note : not execute handler in target thread's context     } } 


Comments