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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
I
InfoQ
B
Blog RSS Feed
D
Docker
GbyAI
GbyAI
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
F
Fortinet All Blogs
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
C
Check Point Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
博客园 - Franky
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Last Week in AI
Last Week in AI
L
LangChain Blog

博客园 - 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.