Ejemplo n.º 1
0
        public void SetCanExecute(bool value)
        {
            if (_canExecute == value)
            {
                return;
            }

            _canExecute = value;
            var handlers = CanExecuteChanged?.GetInvocationList().OfType <EventHandler>().ToArray();

            if (handlers == null)
            {
                return;
            }

            //Parallel.ForEach(handlers, (handler) =>
            foreach (var handler in handlers)
            {
                var syncer = handler.Target as ISynchronizeInvoke;

                if (syncer != null /*&& !dispatcherObject.CheckAccess()*/)
                {
                    //lock (lockDispatch) toDispatch.Add(handler);
                    //Application.Current.Dispatcher
                    syncer.Invoke(handler, new object[0]);
                    //Invoke(DispatcherPriority.DataBind, handler, this, EventArgs.Empty);
                }
                else
                {
                    Application.Current.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this,
                                                          EventArgs.Empty);
                }
                //handler(this, EventArgs.Empty); // note : this does not execute handler in target thread's context
            }
            //CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }