2013-01-10 69 views
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; 
          }); 

回答

7

Afaik沒有可直接詢問調度員隊列長度的屬性或方法。然而,您可能會將處理程序附加到由Hooks屬性提供的DispatcherHooks事件中。

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); 

如果你只能在調度員是否是活動的興趣或沒有,你可能只是在處理會同OperationPosted事件與DispatcherInactive