惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
博客园 - Franky
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
小众软件
小众软件
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
C
Check Point Blog
WordPress大学
WordPress大学
博客园 - 【当耐特】
博客园 - 司徒正美
D
Docker

博客园 - Sheva

Two Xaml Tricks - Sheva I See Cool Stuff Today DataTemplate Is A Bit Naughtier To Play With XmlnsDefinitionAttribute Is Pretty Nifty 征召译者 C# Trivia Test CreateParams Is Pretty Cool! In-Place Editing In WPF SingleLinkedList Implementation Data Binding In WPF C# Application Markup Language Regex 101 Exercise I10 - Extract repeating hex blocks from a string AvalonPaint: Features Added Regex 101 Exercise I9 - Count the number of matches Crossbow: Hosting WPF Controls In Winforms Application Crossbow? WTF? The Architecture Of Avalon Drop Shadow Effect In WPF XamlReader.Load(): Build Up Your Own XamlPad
Application.DoEvents In WPF
Sheva · 2006-08-24 · via 博客园 - Sheva

    Recently, I am tied up writing a custom WPF control called FolderPicker, and I come to the scenario that when I update the data source, I want to let the UI thread to update the control UI, basically, at this time, I should force the dispatcher to process the layout message, in Windows Forms application, If we want to process the windows message such as WM_SIZE, WM_PAINT messages in an event handler, we can call the Application.DoEvents(), then how about in WPF?
    Actually, WPF doesn't provide such an API, the reason is a bit complicated, and really worth a new post contributing to it, then how to implement such kinda API in WPF, after some examination on this problem, and thanks to Kevin Moore's WPF samples code and Ian Griffiths helpful tips, I finally come up with my own implementation of Application.DoEvents logic.
  

using System;

using System.Windows;

using System.Windows.Threading;

namespace Sheva.Windows

{

    /// <summary>

    /// Designates a Windows Presentation Foundation application model with added functionalities.

    /// </summary>

    public class WpfApplication : Application

    {

        private static DispatcherOperationCallback exitFrameCallback = new

                                DispatcherOperationCallback(ExitFrame);

        /// <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();

            }

        }

        private static Object ExitFrame(Object state)

        {

            DispatcherFrame frame = state as DispatcherFrame;

            // Exit the nested message loop.

            frame.Continue = false;

            return null;

        }

    }

}

    The theory behind the code shown above is that when pushing a nested message loop, the nested message loop will immediately process the messages in the queue, so the layout or other window messages can get processed. But you should avoid using nested message loop if at all possible, because as Ian Griffths says:"bad things happen when you nest message pumps." what does this mean? let me save it for another day:)
    Edit: Jessica Fosler has wrriten an awesome article on Application.DoEvents which is really worth a read.