Application.DoEvents 의 부작용

seung-jae hwang·2019년 4월 23일
0

C샵

목록 보기
8/17

From : https://www.sysnet.pe.kr/2/1/992

Keeping your UI Responsive and the Dangers of Application.DoEvents
; http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx

윈도우 환경에서 개발하는 한, 죽을 때까지 ^^ 프로그래머를 따라다닐 메시지 루프이기 때문에. 꼭 시간내서 읽어보세요. 참고로 WPF 에는 DoEvents 가 없어졌지요. 아래의 글에서는 이에 대한 동일한 로직을 제시하고 있습니다.

Application.DoEvents In WPF
; http://www.cnblogs.com/sheva/archive/2006/08/24/485790.html

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.

分类: Avalon

0개의 댓글