gpt4 book ai didi

wpf - 查询调度队列长度

转载 作者:行者123 更新时间:2023-12-04 16:02:35 24 4
gpt4 key购买 nike

我正在尝试分析 UI 线程的使用情况。是否可以查询调度员排队的项目数?

更新 :Clemens 的回答完美无缺,但是因为我想在 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;
});

最佳答案

Afaik 没有属性或方法可以直接询问调度程序队列长度。但是,您可以将处理程序附加到某些 DispatcherHooks事件,由 Hooks 提供属性(property)。

var queueLength = 0;
Dispatcher.Hooks.OperationPosted += (o, e) => Interlocked.Increment(ref queueLength);
Dispatcher.Hooks.OperationStarted += (o, e) => Interlocked.Decrement(ref queueLength);
Dispatcher.Hooks.OperationAborted += (o, e) => Interlocked.Decrement(ref queueLength);

如果您只对 Dispatcher 是否处于事件状态感兴趣,您可能只需处理 OperationPostedDispatcherInactive 一起举办的事件.

关于wpf - 查询调度队列长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14260857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com