相关疑难解决方法(0)

查询Dispatcher队列长度

我正在尝试分析UI线程的用法.是否可以查询调度员排队的物品数量?

更新:克莱门斯的答案是完美的,但是因为我想在UI启动后启动它,我只关心每秒采样一次我使用以下代码...

        int queueLength = 0;
        var currentDispatcher = Dispatcher.CurrentDispatcher;
        currentDispatcher.Hooks.OperationPosted += (s, e) => Interlocked.Increment(ref queueLength);
        currentDispatcher.Hooks.OperationCompleted += (s, e) => Interlocked.Decrement(ref queueLength);
        currentDispatcher.Hooks.OperationAborted += (s, e) => Interlocked.Decrement(ref queueLength);
        Observable
            .Interval(TimeSpan.FromSeconds(1))
            .Subscribe(x =>
                           {
                               int currentQueueLength = queueLength;
                               if (currentQueueLength < 0)
                               {
                                   Interlocked.Add(ref queueLength, currentQueueLength * -1);
                               }
                               UiQueueLength = queueLength;
                           });
Run Code Online (Sandbox Code Playgroud)

wpf

5
推荐指数
1
解决办法
2681
查看次数

标签 统计

wpf ×1