4
我在分析UI線程的用法。是否可以查詢調度員排隊的項目數量?查詢調度隊列長度
UPDATE:克萊門斯回答完美的作品,但我想踢這個離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;
});