protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (!this.suspendCollectionChangeNotification) { NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler != null) { Delegate[] delegates = eventHandler.GetInvocationList(); bool isEventInvoked = false; foreach (NotifyCollectionChangedEventHandler handler in delegates) { isEventInvoked = false; if (handler.Target is DispatcherObject) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && dispatcherObject.CheckAccess() == false) { dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, handler, this, e); isEventInvoked = true; } } if (!isEventInvoked) { handler(this, e); } } } } }
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (this.CollectionChanged != null) { try { // Walk thru invocation list foreach (NotifyCollectionChangedEventHandler handler in CollectionChanged.GetInvocationList()) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; // If the subscriber is a DispatcherObject and different thread if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { // Invoke handler in the target dispatcher's thread dispatcherObject.Dispatcher.BeginInvoke(DispatcherPriority.DataBind, handler, this, e); } else // Execute handler as is { handler(this, e); } } } catch (Exception err) { } } }
public override void OnInvocation(MethodInvocationEventArgs eventArgs) { DispatcherObject dispatcherObject = (eventArgs.Delegate.Target ?? eventArgs.GetArgumentArray()[0]) as DispatcherObject; if (dispatcherObject != null) { if (dispatcherObject.CheckAccess()) { // We are already in the GUI thread. Proceed. eventArgs.Proceed(); } else { if (Async) { // Invoke the target method asynchronously (don't wait). dispatcherObject.Dispatcher.BeginInvoke(Priority, new Action(() => eventArgs.Proceed())); } else { // Invoke the target method synchronously. dispatcherObject.Dispatcher.Invoke( Priority, new Action(() => eventArgs.Proceed())); } } } else { eventArgs.Proceed(); } }
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { try { using (base.BlockReentrancy()) { NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged; if (collectionChanged != null) { foreach (NotifyCollectionChangedEventHandler notifyCollectionChangedEventHandler in collectionChanged.GetInvocationList()) { DispatcherObject dispatcherObject = notifyCollectionChangedEventHandler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, notifyCollectionChangedEventHandler, this, new object[] { e }); } else { notifyCollectionChangedEventHandler(this, e); } } } } } catch (Exception) { //Log.WarningException("ObservableCollectionEx.OnCollectionChanged()", exception); } }
private void OnCollectionChanged(NotifyCollectionChangedAction action, IList changedItems) { NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(action, changedItems); Delegate[] delegates = eventHandler.GetInvocationList(); // Walk thru invocation list foreach (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(NotifyCollectionChangedEventArgs e) { using (BlockReentrancy()) { NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); foreach (NotifyCollectionChangedEventHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && dispatcherObject.CheckAccess() == false) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e); } else { handler(this, e); } } } }
public override void OnInvoke(MethodInterceptionArgs eventArgs) { DispatcherObject dispatcherObject = (DispatcherObject)(eventArgs.Instance ?? eventArgs.Arguments.GetArgument(0)); if (dispatcherObject == null || dispatcherObject.CheckAccess()) { // We are already in the GUI thread. Proceed. eventArgs.Proceed(); } else { if (this.Async) { // Invoke the target method asynchronously (don't wait). dispatcherObject.Dispatcher.BeginInvoke(this.Priority, new Action(eventArgs.Proceed)); } else { // Invoke the target method synchronously. dispatcherObject.Dispatcher.Invoke( this.Priority, new Action(eventArgs.Proceed)); } } }
// Event Handler protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { // Be nice - use BlockReentrancy like MSDN said using (BlockReentrancy()) { if (CollectionChanged != null) { // Walk thru invocation list foreach (NotifyCollectionChangedEventHandler handler in CollectionChanged.GetInvocationList()) { 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.Invoke(DispatcherPriority.DataBind, handler, this, e); } else // Execute handler as is { handler(this, e); } } } }
public static void SetAccess(this DispatcherObject d) { if (d.CheckAccess()) { return; } Dispatcher dispatcher = d.Dispatcher; FieldInfo dispatcherThreadField = typeof(Dispatcher).GetField("_dispatcherThread", BindingFlags.Instance | BindingFlags.NonPublic); dispatcherThreadField.SetValue(dispatcher, Thread.CurrentThread); }
public static TResult Invoke <TResult>(this DispatcherObject obj, Func <TResult> action) { if (obj == null) { return(default(TResult)); } if (obj.CheckAccess()) { return(action()); } return((TResult)obj.Dispatcher.Invoke(DispatcherPriority.Normal, action)); }
/// <summary> /// [UI线程]必要时在指定控件的创建线程调用代理 /// </summary> /// <typeparam name="T">控件基类</typeparam> /// <param name="ctrl">控件</param> /// <param name="obj"></param> /// <param name="a"></param> public static void Action_UI <T>(DispatcherObject ctrl, T obj, System.Action <T> a) { if (ctrl.CheckAccess()) { a(obj); } else { object[] Params = { obj }; ctrl.Dispatcher.Invoke(a, Params); } System.Windows.Forms.Application.DoEvents(); }
/// <summary> /// This collection changed event performs thread safe event raising. /// </summary> /// <param name="e">The event argument.</param> protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { // Recommended is to avoid reentry // in collection changed event while collection // is getting changed on other thread. using (BlockReentrancy()) { if (!suspendCollectionChangeNotification) { NotifyCollectionChangedEventHandler eventHandler = CollectionChanged; if (eventHandler == null) { return; } // Walk thru invocation list. Delegate[] delegates = eventHandler.GetInvocationList(); foreach (NotifyCollectionChangedEventHandler handler in delegates) { // If the subscriber is a DispatcherObject and different thread. DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null) { if (!dispatcherObject.CheckAccess()) { // Invoke handler in the target dispatcher's thread... // asynchronously for better responsiveness. dispatcherObject.Dispatcher.BeginInvoke (DispatcherPriority.DataBind, handler, this, e); } else { // Execute handler as is. handler(this, e); } } else { // Execute handler as is. handler(this, e); } } } } }
public static void Invoke(this DispatcherObject obj, Action action) { if (obj == null) { return; } if (obj.CheckAccess()) { action(); } else { obj.Dispatcher.Invoke(DispatcherPriority.Normal, action); } }
public override void OnInvokeHandler(EventInterceptionArgs args) { DispatcherObject dispatcherObject = args.Handler.Target as DispatcherObject; if (dispatcherObject == null || dispatcherObject.CheckAccess()) { args.ProceedInvokeHandler(); } else { // We have to dispatch synchronously to avoid the object to be changed // before the time the event is raised and the time it is processed. dispatcherObject.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(args.ProceedInvokeHandler)); } }
public static void Invoke(Action action) { DispatcherObject dispatcher = Application.Current; if (dispatcher == null || dispatcher.CheckAccess() || dispatcher.Dispatcher == null ) { action(); } else { SafeInvoke(action); } }
public static T Invoke <T>(this DispatcherObject dispatcher, Func <T> func) { try { if (dispatcher.CheckAccess()) { return(func()); } else { return(dispatcher.Dispatcher.Invoke(func)); } } catch (TaskCanceledException ex) { Console.WriteLine(ex); Console.WriteLine("执行代码中,控件可能已释放..."); return(default);
protected void OnControllerAxisChanged(ControllerJoystick joystick, ControllerJoystickAxis axis, byte oldValue, byte newValue) { if (AxisChanged != null) { ControllerAxisChangedHandler eventHandler = AxisChanged; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (ControllerAxisChangedHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, joystick, axis, oldValue, newValue); } else { handler(joystick, axis, oldValue, newValue); } } } }
protected void OnFrameReady(IFrame frame) { if (FrameReady != null) { DeviceFrameReadyHandler eventHandler = FrameReady; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (DeviceFrameReadyHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, frame); } else { handler(this, frame); } } } }
public override void OnInvoke(MethodInterceptionArgs args) { DispatcherObject dispatcherObject = (DispatcherObject)args.Instance; if (this.Async) { // Invoke the method asynchronously on the GUI thread. dispatcherObject.Dispatcher.BeginInvoke(this.priority, new Action(args.Proceed)); } else if (dispatcherObject.CheckAccess()) { // We have access to the GUI object. Invoke the method synchronously. args.Proceed(); } else { // We don't have access to the GUI thread. Invoke the method synchronously on that thread. dispatcherObject.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(args.Proceed)); } }
protected void OnButtonStateChanged(Button button, bool pressed) { if (ButtonStateChanged != null) { ButtonStateChangedHandler eventHandler = ButtonStateChanged; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (ButtonStateChangedHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, button, pressed); } else { handler(button, pressed); } } } }
private void OnBitmapFrameCaptured(BitmapFrame frame) { if (BitmapFrameCaptured != null) { BitmapFrameCapturedHandler eventHandler = BitmapFrameCaptured; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (BitmapFrameCapturedHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, frame); } else { handler(frame); } } } }
protected void OnControllerConnectionChanged(bool connected) { if (ConnectionChanged != null) { ControllerConnectionChangedHandler eventHandler = ConnectionChanged; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (ControllerConnectionChangedHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, connected); } else { handler(connected); } } } }
public static T SafeCreateInstance <T>(this DispatcherObject dispatcherObject, params object[] args) { try { var type = typeof(T); const BindingFlags bf = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.NonPublic; if (dispatcherObject.CheckAccess()) { return((T)Activator.CreateInstance(type, bf, null, args, null)); } T ret = default(T); dispatcherObject.Dispatcher.Invoke(new Action(delegate { ret = (T)Activator.CreateInstance(type, bf, null, args, null); })); return(ret); } catch (Exception ex) { ExceptionHandler.ThrowException(ex); throw; } }
protected void OnCameraConnectionChanged(IntPtr context, tInterface iface, tLinkEvent evt, UInt32 uniqueId) { bool connected = (evt == tLinkEvent.eLinkAdd); Camera camera; if (connected) { tCameraInfo cameraInfo = new tCameraInfo(); tErr err = Pv.CameraInfo(uniqueId, ref cameraInfo); if (err != tErr.eErrSuccess) { throw new PvException(err); } camera = new Camera(cameraInfo); cameras.Add(camera); } else { camera = cameras.Find(c => c.UniqueId == uniqueId); } if (camera != null && (CameraConnected != null && connected) || (CameraDisconnected != null && !connected)) { CameraConnectionHandler eventHandler = connected ? CameraConnected : CameraDisconnected; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (CameraConnectionHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, camera); } else { handler(camera); } } } }
void FirePropertyChanged(string strName) { if (PropertyChanged != null) { #if WINDOWS_PHONE System.Windows.Deployment.Current.Dispatcher.BeginInvoke(PropertyChanged, this, new System.ComponentModel.PropertyChangedEventArgs(strName)); #elif MONO PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(strName)); #else System.ComponentModel.PropertyChangedEventArgs args = new System.ComponentModel.PropertyChangedEventArgs(strName); System.ComponentModel.PropertyChangedEventHandler eventHandler = PropertyChanged; if (eventHandler == null) { return; } Delegate[] delegates = eventHandler.GetInvocationList(); // Walk thru invocation list foreach (System.ComponentModel.PropertyChangedEventHandler 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); } } #endif } //if (PropertyChanged != null) // PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(strName)); }
public static void Invoke(this DispatcherObject dispatcher, Action action) { try { if (dispatcher.CheckAccess()) { action(); } else { dispatcher.Dispatcher.Invoke(action); } } catch (TaskCanceledException ex) { Console.WriteLine(ex); Console.WriteLine("执行代码中,控件可能已释放..."); } catch { throw; } }
protected void OnMessageReceived(byte[] message) { if (MessageReceived == null) { return; } DeviceMessageHandler eventHandler = MessageReceived; Delegate[] delegates = eventHandler.GetInvocationList(); foreach (DeviceMessageHandler handler in delegates) { DispatcherObject dispatcherObject = handler.Target as DispatcherObject; if (dispatcherObject != null && !dispatcherObject.CheckAccess()) { dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, message); } else { handler(this, message); } } }
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 } }