且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在单元测试中使用WPF调度

更新时间:2023-02-21 09:32:44

通过使用Visual Studio单元测试框架,你不需要初始化调度自己。你是绝对正确的,该调度程序不会自动处理它的队列。

By using the Visual Studio Unit Test Framework you don’t need to initialize the Dispatcher yourself. You are absolutely right, that the Dispatcher doesn’t automatically process its queue.

您可以编写一个简单的辅助方法DispatcherUtil.DoEvents(),它告诉调度员来处理它的队列。

You can write a simple helper method "DispatcherUtil.DoEvents()" which tells the Dispatcher to process its queue.

C#code:

public static class DispatcherUtil
{
    [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public static void DoEvents()
    {
        DispatcherFrame frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            new DispatcherOperationCallback(ExitFrame), frame);
        Dispatcher.PushFrame(frame);
    }

    private static object ExitFrame(object frame)
    {
        ((DispatcherFrame)frame).Continue = false;
        return null;
    }
}

您在找到该类太 WPF应用程序框架(WAF)

You find this class too in the WPF Application Framework (WAF).