Querying the Dispatcher Queue Length

后端 未结 1 1918
猫巷女王i
猫巷女王i 2021-01-06 01:23

I\'m trying to analyse the usage of the UI thread. Is it possible to query the number of items queued by the dispatcher?

UPDATE: Clemens answer wor

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 02:15

    Afaik there is no property or method where you can directly ask for the dispatcher queue length. You may however attach handlers to some of the DispatcherHooks events, provided by the 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);
    

    If you are only interested in whether the Dispatcher is active or not, you might just handle the OperationPosted event in conjunction with DispatcherInactive.

    0 讨论(0)
提交回复
热议问题