2013-01-10 2 views
4

UI 스레드의 사용법을 분석하려고합니다. 발송자가 대기중인 항목 수를 쿼리 할 수 ​​있습니까?Dispatcher 큐 길이 쿼리

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 이벤트 중 일부에 처리기를 연결할 수 있습니다.


경우에만 Dispatcher가 활성 상태인지 여부에 관심이 또는없는 경우

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

, 당신은 단지 DispatcherInactive와 관련하여 OperationPosted 이벤트를 처리 할 수 ​​있습니다.