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);
                        }
                    }
                }
            }
        }
Exemple #2
0
 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) { }
     }
 }
Exemple #3
0
        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));
                }
            }
        }
Exemple #8
0
 // 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);
                 }
             }
         }
     }
 }
Exemple #9
0
        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);
                    }
                }
            }
        }
Exemple #10
0
    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);
    }
Exemple #11
0
 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));
 }
Exemple #12
0
        /// <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();
        }
Exemple #13
0
        /// <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);
                        }
                    }
                }
            }
        }
Exemple #14
0
 public static void Invoke(this DispatcherObject obj, Action action)
 {
     if (obj == null)
     {
         return;
     }
     if (obj.CheckAccess())
     {
         action();
     }
     else
     {
         obj.Dispatcher.Invoke(DispatcherPriority.Normal, action);
     }
 }
Exemple #15
0
        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);
            }
        }
Exemple #17
0
 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);
Exemple #18
0
 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);
             }
         }
     }
 }
Exemple #19
0
 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);
             }
         }
     }
 }
Exemple #20
0
        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));
            }
        }
Exemple #21
0
 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);
             }
         }
     }
 }
Exemple #22
0
 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);
             }
         }
     }
 }
Exemple #23
0
 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);
             }
         }
     }
 }
Exemple #24
0
        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;
            }
        }
Exemple #25
0
        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);
                    }
                }
            }
        }
Exemple #26
0
        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));
        }
Exemple #27
0
 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;
     }
 }
Exemple #28
0
        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);
                }
            }
        }
Exemple #29
0
        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
            }
        }