protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { //Limito l'accesso da parte di altri Thread che vogliono modificare la collection using (BlockReentrancy()) { System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); // Walk thru invocation list foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; // If the subscriber is a DispatcherObject and different thread if (dispatcherObject != null && dispatcherObject.CheckAccess() == false) { // Invoke handler in the target dispatcher's thread dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e); } else // Execute handler as is { handler(this, e); } } } }
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // Be nice - use BlockReentrancy like MSDN said using (BlockReentrancy()) { System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); // Walk thru invocation list foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; // If the subscriber is a DispatcherObject and different thread if (dispatcherObject != null && dispatcherObject.CheckAccess() == false) { // Invoke handler in the target dispatcher's thread dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, handler, this, e); dispatcherObject.Dispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(DispatcherUnhandledException); dispatcherObject.Dispatcher.UnhandledExceptionFilter += new DispatcherUnhandledExceptionFilterEventHandler(DispatcherUnhandledExceptionFilter); } else // Execute handler as is { handler(this, e); } } } }
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // Be nice - use BlockReentrancy like MSDN said using (BlockReentrancy()) { System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); // Walk thru invocation list foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; // If the subscriber is a DispatcherObject and different thread if (dispatcherObject != null) { // Invoke handler in the target dispatcher's thread dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e); } } } }
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { using (BlockReentrancy()) { System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e); } else { handler(this, e); } } } }
void FireCollectionChanged(object obj, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) { if (CollectionChanged != null) { #if WINDOWS_PHONE System.Windows.Deployment.Current.Dispatcher.BeginInvoke(new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SafeFireCollectionChanged), this, args); #elif MONO CollectionChanged(obj, args); #else System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); // Walk thru invocation list foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; // If the subscriber is a DispatcherObject and different thread if (dispatcherObject != null && dispatcherObject.CheckAccess() == false) { // Invoke handler in the target dispatcher's thread dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, args); } else // Execute handler as is { handler(this, args); } } //System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SafeFireCollectionChanged), // this, args); #endif } }