Example #1
0
        /// <summary>
        /// If DispatcherProcessing is disabled, this will BeginInvoke the appropriate KB operation
        /// for later processing.  It also will cancel any pending operations so only one op can be in
        /// flight at a time.
        /// </summary>
        /// <param name="kbCall">The kb function to call.</param>
        /// <param name="focusedObject">The object being focused</param>
        /// <returns>True if an operation has been scheduled, false otherwise</returns>
        private static bool CheckAndDispatchKbOperation(Action <DependencyObject> kbCall, DependencyObject focusedObject)
        {
            // Abort the current operation if we reach another call and it is
            // still pending.
            if (s_KbOperation?.Status == DispatcherOperationStatus.Pending)
            {
                s_KbOperation.Abort();
            }

            s_KbOperation = null;

            // Don't call any KB operations under disabled processing as a COM wait could
            // cause re-entrancy and an InvalidOperationException.
            if (Dispatcher.CurrentDispatcher._disableProcessingCount > 0)
            {
                // Retry when processing resumes
                s_KbOperation =
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Input,
                                                             new Action(() =>
                {
                    // Since this action is happening sometime later, the focus
                    // may have already changed.
                    if (Keyboard.FocusedElement == focusedObject)
                    {
                        kbCall(focusedObject);
                    }
                }));

                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Processes all UI messages currently in the message queue.
        /// </summary>
        public static void DoEvents()
        {
            // Create new nested message pump.
            DispatcherFrame nestedFrame = new DispatcherFrame();



            // Dispatch a callback to the current message queue, when getting called,
            // this callback will end the nested message loop.
            // note that the priority of this callback should be lower than the that of UI event messages.
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
                DispatcherPriority.Background, exitFrameCallback, nestedFrame);



            // pump the nested message loop, the nested message loop will
            // immediately process the messages left inside the message queue.
            Dispatcher.PushFrame(nestedFrame);



            // If the "exitFrame" callback doesn't get finished, Abort it.
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
        public void Dispose()
        {
            VerifyAccess();

            using (m_lockHelper.GetLock())
            {
                if (!m_disposed)
                {
                    m_disposed = true;
                    m_lockHelper.Pulse();
                }
            }

            if (m_thread != null)
            {
                // Must do this assignment, because m_operation could be set to null
                //  between the null check and the call to abort.
                DispatcherOperation operation = m_operation;
                if (operation != null)
                {
                    operation.Abort();
                }

                m_thread.Join();
            }
        }
Example #4
0
        /// <summary>
        ///   Processes all UI messages currently in the message queue.
        /// </summary>
        private static void RenderThenExecute(DispatcherPriority priority)
        {
            try
            {
                // Create new nested message pump.
                DispatcherFrame nestedFrame = new DispatcherFrame();

                // Dispatch a callback to the current message queue, when getting called,
                // this callback will end the nested message loop.
                // The priority of this callback should be lower than that of event message you want to process.
                DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(priority, _exitFrameCallback, nestedFrame);

                // pump the nested message loop, the nested message loop will immediately
                // process the messages left inside the message queue.
                Dispatcher.PushFrame(nestedFrame);

                // If the "exitFrame" callback is not finished, abort it.
                if (exitOperation.Status != DispatcherOperationStatus.Completed)
                {
                    exitOperation.Abort();
                }
            }
            // threads can be out of sync depending on usage, but don't let that kill the app - bbulla
            catch
            {}
        }
Example #5
0
 void InvalidateMeasure(DispatcherPriority priority)
 {
     if (priority >= DispatcherPriority.Render)
     {
         if (invalidateMeasureOperation != null)
         {
             invalidateMeasureOperation.Abort();
             invalidateMeasureOperation = null;
         }
         base.InvalidateMeasure();
         base.InvalidateVisual();
     }
     else
     {
         if (invalidateMeasureOperation != null)
         {
             invalidateMeasureOperation.Priority = priority;
         }
         else
         {
             invalidateMeasureOperation = Dispatcher.BeginInvoke(
                 priority,
                 new Action(
                     delegate
             {
                 invalidateMeasureOperation = null;
                 base.InvalidateMeasure();
                 base.InvalidateVisual();
             }
                     )
                 );
         }
     }
 }
Example #6
0
        private static bool WaitForPriority(DispatcherPriority priority)
        {
            const int defaultTimeout = 30000;

            // Schedule the ExitFrame operation to end the nested pump after the timeout trigger happens
            TimeoutFrame frame = new TimeoutFrame();

            FrameTimer timeoutTimer = new FrameTimer(frame, defaultTimeout,
                                                     TimeoutFrameOperationInstance, DispatcherPriority.Send);

            timeoutTimer.Start();

            // exit after a priortity has been processed
            DispatcherOperation opExit = Dispatcher.CurrentDispatcher.BeginInvoke(priority,
                                                                                  ExitFrameOperationInstance, frame);

            // Pump the dispatcher
            Dispatcher.PushFrame(frame);

            // abort the operations that did not get processed
            if (opExit.Status != DispatcherOperationStatus.Completed)
            {
                opExit.Abort();
            }
            if (!timeoutTimer.IsCompleted)
            {
                timeoutTimer.Stop();
            }

            return(!frame.TimedOut);
        }
Example #7
0
        /// <summary>現在メッセージ待ち行列の中にある全UIメッセージを処理する</summary>
        private void DoEvents()
        {
            // 新しくネスト化されたメッセージ ポンプを作成
            DispatcherFrame frame = new DispatcherFrame();

            // DispatcherFrame (= 実行ループ) を終了させるコールバック
            DispatcherOperationCallback exitFrameCallback = (f) =>
            {
                // ネスト化されたメッセージ ループを抜ける
                ((DispatcherFrame)f).Continue = false;
                return(null);
            };

            // 非同期で実行する
            // 優先度を Background にしているので、このコールバックは
            // ほかに処理するメッセージがなくなったら実行される
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
                DispatcherPriority.Background, exitFrameCallback, frame);

            // 実行ループを開始する
            Dispatcher.PushFrame(frame);

            // コールバックが終了していない場合は中断
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
Example #8
0
        public void Wait()
        {
            // see https://stackoverflow.com/questions/35172613/invalidoperationexception-dispatcher-processing-has-been-suspended-but-message
            // If we're on the UI thread already, there is little point to this call b/c the UI pump has finished processing previous messages
            // and is ready to process the current one.
            // Furthermore, if the current thread is the UI thread and then tries to re-enter the UI thread, you can get an InvalidOperationException, which is thrown
            // b/c of re-entrancy checks in the Dispatcher.
            if (UiDispatcher == null || UiDispatcher.CheckAccess())
            {
                return;
            }

            try
            {
                // Create new nested message pump.
                DispatcherFrame nestedFrame = new DispatcherFrame();

                // Dispatch a callback to the current message queue, when getting called,
                // this callback will end the nested message loop.
                // The priority of this callback should be lower than that of event message you want to process.
                DispatcherOperation exitOperation = UiDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, ExitFrameCallback, nestedFrame);

                // pump the nested message loop, the nested message loop will immediately
                // process the messages left inside the message queue.
                Dispatcher.PushFrame(nestedFrame);

                // If the "exitFrame" callback is not finished, abort it.
                if (exitOperation.Status != DispatcherOperationStatus.Completed)
                {
                    exitOperation.Abort();
                }
            }
            // threads can be out of sync depending on usage, but don't let that kill the app
            catch { }
        }
Example #9
0
 private void Op_Completed(object sender, EventArgs e)
 {
     if (op.Status == DispatcherOperationStatus.Completed)
     {
         doc.DataContext = null;
         op.Abort();
     }
 }
 private void Op_Completed(object sender, EventArgs e)
 {
     if (op.Status == DispatcherOperationStatus.Completed) //如果操作完成
     {
         doc.DataContext = null;                           //清空
         op.Abort();                                       //终止操作
     }
 }
Example #11
0
 /// <summary>
 ///     Cancels a pending DispatcherOperation
 /// </summary>
 internal void Cancel()
 {
     if (IsPending)
     {
         _operation.Abort();
     }
     _operation = null;
 }
 private void CancelSubscribe()
 {
     if (subscribing != null && subscribing.Status == DispatcherOperationStatus.Pending)
     {
         subscribing.Abort();
         subscribing = null;
     }
 }
Example #13
0
 public virtual void Cancel()
 {
     CancellationPending = true;
     if (_latestOperation.Status == DispatcherOperationStatus.Executing ||
         _latestOperation.Status == DispatcherOperationStatus.Pending)
     {
         _latestOperation.Abort();
     }
 }
Example #14
0
 internal void Cancel()
 {
     if (_operation != null)
     {
         Debug.Assert(_operation.Status == DispatcherOperationStatus.Pending);
         _operation.Abort();
         _operation = null;
     }
 }
        public static void WaitForPriority(DispatcherPriority priority)
        {
            DispatcherFrame     frame = new DispatcherFrame();
            DispatcherOperation dispatcherOperation = Dispatcher.CurrentDispatcher.BeginInvoke(priority, new DispatcherOperationCallback(ExitFrameOperation), frame);

            Dispatcher.PushFrame(frame);
            if (dispatcherOperation.Status != DispatcherOperationStatus.Completed)
            {
                dispatcherOperation.Abort();
            }
        }
Example #16
0
        public static void DoEvents()
        {
            var nestedFrame = new DispatcherFrame();
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), nestedFrame);

            Dispatcher.PushFrame(nestedFrame);
            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
        public static void DoEvents(this Application app, DispatcherPriority priority = DispatcherPriority.Background)
        {
            DispatcherFrame     nestedFrame   = new DispatcherFrame();
            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(priority, exitFrameCallback, nestedFrame);

            try
            {
                //execute all next message
                Dispatcher.PushFrame(nestedFrame);

                //If not completed, will stop it
                if (exitOperation.Status != DispatcherOperationStatus.Completed)
                {
                    exitOperation.Abort();
                }
            }
            catch
            {
                exitOperation.Abort();
            }
        }
        private void QueueRenderText()
        {
            if (CurrentDispatcherOperation != null)
            {
                CurrentDispatcherOperation.Abort();
            }

            CurrentDispatcherOperation = Dispatcher.BeginInvoke(RenderTextAction, DispatcherPriority.Render, null);

            CurrentDispatcherOperation.Aborted   += (o, e) => { CurrentDispatcherOperation = null; };
            CurrentDispatcherOperation.Completed += (o, e) => { CurrentDispatcherOperation = null; };
        }
Example #19
0
 /// <summary>
 ///     Moves the window back to its original position.
 ///     Used to provide feedback that panning has hit an edge.
 /// </summary>
 /// <param name="animateBack">Whether to animate or snap back to the original position</param>
 public void EndPanningFeedback(bool animateBack)
 {
     if (_hwndSource != null && _isProvidingPanningFeedback)
     {
         _isProvidingPanningFeedback = false;
         if (_updatePanningOperation != null)
         {
             _updatePanningOperation.Abort();
             _updatePanningOperation = null;
         }
         UnsafeNativeMethods.EndPanningFeedback(Handle, animateBack);
     }
 }
 // Token: 0x06000306 RID: 774 RVA: 0x00008630 File Offset: 0x00006830
 internal static void RemoveUnloadedCallback(DependencyObject d, object[] unloadedPending)
 {
     if (unloadedPending != null)
     {
         d.ClearValue(FrameworkElement.UnloadedPendingPropertyKey);
         DispatcherOperation dispatcherOperation = (DispatcherOperation)unloadedPending[1];
         if (dispatcherOperation.Status == DispatcherOperationStatus.Pending)
         {
             dispatcherOperation.Abort();
         }
         MediaContext.From(d.Dispatcher).RemoveLoadedOrUnloadedCallback((LoadedOrUnloadedOperation)unloadedPending[0]);
     }
 }
Example #21
0
        /// <summary>
        ///
        /// </summary>
        private async void PositionTask()
        {
            if (m_operation != null && m_operation.Status != DispatcherOperationStatus.Completed)
            {
                m_operation.Abort();
            }

            await Task.Delay(800);

            m_operation = Application.Current.Dispatcher.BeginInvoke(
                new Action(() => StartValidation()),
                DispatcherPriority.Background);
        }
Example #22
0
        public static void DoEvents()
        {
            var frame = new DispatcherFrame();
            DispatcherOperation dispatcherOperation =
                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, ExitFrameCallback, frame);

            Dispatcher.PushFrame(frame);
            if (dispatcherOperation.Status == DispatcherOperationStatus.Completed)
            {
                return;
            }
            dispatcherOperation.Abort();
        }
        protected override void OnContentChanged(object oldContent, object newContent)
        {
            if (_currentContentPresenter == null)
            {
                return;
            }

            var valueChanged = !Equals(oldContent, newContent);

            if (valueChanged)
            {
                base.OnContentChanged(oldContent, newContent);

                if (_isInTransition || (_hideStoryboard == null && _showStoryboard == null))
                {
                    _currentContentPresenter.Content = newContent;
                }
                else
                {
                    if (_changeAnimationTask != null)
                    {
                        _changeAnimationTask.Abort();
                    }

                    _changeAnimationTask = Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (_hideStoryboard != null)
                        {
                            _hideStoryboard.CurrentStateInvalidated += _hideStoryboard_CurrentStateInvalidated;
                            _hideStoryboard.Begin(_currentContentPresenter, true);
                            _hideStoryboardCompleted = false;

                            this.SwapContentPresenters();
                        }

                        _currentContentPresenter.Content = newContent;

                        if (_showStoryboard != null)
                        {
                            _showStoryboard.CurrentStateInvalidated += _showStoryboard_CurrentStateInvalidated;
                            _showStoryboard.Begin(_currentContentPresenter, true);
                            _showStoryboardCompleted = false;
                        }

                        _isInTransition = true;

                        _changeAnimationTask = null;
                    }), DispatcherPriority.Background);
                }
            }
        }
Example #24
0
        private void InvalidateContainers()
        {
            if (_currentOperation != null)
            {
                _currentOperation.Abort();
            }

            Action action = delegate
            {
                _currentOperation = null;
                GenerateContainers();
            };

            _currentOperation = Dispatcher.BeginInvoke(action, DispatcherPriority.Normal);
        }
Example #25
0
        /// <summary>
        /// 刷新WPF界面
        /// </summary>
        public static void Refresh()
        {
            DispatcherFrame     nestedFrame   = new DispatcherFrame();
            DispatcherOperation exitOperation =
                Dispatcher.CurrentDispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    exitFrameCallback, nestedFrame);

            Dispatcher.PushFrame(nestedFrame);

            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
Example #26
0
        /// <summary>
        /// Synchronise les sélections.
        /// </summary>
        /// <param name="sourceIsThis"><c>true</c> si la synchro provient des <see cref="SelectedItems"/> de cette instance.</param>
        private void Sync(bool sourceIsThis)
        {
            if (!DesignMode.IsInDesignMode)
            {
                if (_isSyncing)
                {
                    if (_syncOp != null && !sourceIsThis)
                    {
                        _syncOp.Abort();
                    }
                    else
                    {
                        return;
                    }
                }

                _isSyncing = true;

                if (sourceIsThis)
                {
                    _syncOp = Dispatcher.BeginInvoke((Action)(() =>
                    {
                        if (IsSelectionModeSingle())
                        {
                            if (this.SelectedItems != null)
                            {
                                this.AssociatedObject.SelectedItem = this.SelectedItems.Cast <object>().FirstOrDefault();
                            }
                            else
                            {
                                this.AssociatedObject.SelectedItem = null;
                            }
                        }
                        else
                        {
                            Sync(this.SelectedItems, AssociatedObjectSelectedItems);
                        }

                        _isSyncing = false;
                    }), System.Windows.Threading.DispatcherPriority.Loaded);
                }
                else
                {
                    Sync(AssociatedObjectSelectedItems, this.SelectedItems);
                    _isSyncing = false;
                }
            }
        }
Example #27
0
        public static void DoEvents(DispatcherPriority targetPriority)
        {
            DispatcherFrame     nestedFrame = new DispatcherFrame();
            DispatcherOperation exitOperation
                = Dispatcher.CurrentDispatcher.BeginInvoke(targetPriority,
                                                           new DispatcherOperationCallback(obj => ((DispatcherFrame)obj).Continue = false),
                                                           nestedFrame);

            try { Dispatcher.PushFrame(nestedFrame); }
            catch { }

            if (exitOperation.Status != DispatcherOperationStatus.Completed)
            {
                exitOperation.Abort();
            }
        }
Example #28
0
 internal void UpdateVisible()
 {
     if (updateVisibleCounter == 0)
     {
         UpdateVisibleBody();
     }
     else if (updateVisibleOperation == null)
     {
         updateVisibleOperation = Dispatcher.BeginInvoke(() => UpdateVisibleBody(), invocationPriority);
         return;
     }
     else if (updateVisibleOperation.Status == DispatcherOperationStatus.Pending)
     {
         updateVisibleOperation.Abort();
         updateVisibleOperation = Dispatcher.BeginInvoke(() => UpdateVisibleBody(), invocationPriority);
     }
 }
Example #29
0
 public static void DoEvents()
 {
     try
     {
         DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
         DispatcherFrame             nestedFrame       = new DispatcherFrame();
         DispatcherOperation         exitOperation     = DispatcherHelper.UI_Dispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
         Dispatcher.PushFrame(nestedFrame);
         if (exitOperation.Status != DispatcherOperationStatus.Completed)
         {
             exitOperation.Abort();
         }
     }
     catch (Exception ex)
     {
     }
 }
            /// <summary>
            /// Starts a new refresh operation.
            /// </summary>
            /// <remarks>
            /// CAUTION This method must be called on the <see cref="Dispatcher"/>'s thread.
            /// </remarks>
            void StartRefreshValues()
            {
                if (_refreshOperation != null)
                {
                    // The previously scheduled refresh operation must be aborted since values must be refreshed from the start
                    _refreshOperation.Abort();
                    _refreshOperation = null;
                }

                if (_localizedValueList.First != null)
                {
                    // Schedule the first phase of the refresh operation
                    _refreshOperation = Dispatcher.BeginInvoke(DispatcherPriority.Background, new SendOrPostCallback(RefreshValues), _localizedValueList.First);
                }

                // Allow new refresh operations to be scheduled
                Interlocked.Exchange(ref _refreshScheduled, 0);
            }