public CoreDispatcher() { _highSyncCtx = new CoreDispatcherSynchronizationContext(this, CoreDispatcherPriority.High); _normalSyncCtx = new CoreDispatcherSynchronizationContext(this, CoreDispatcherPriority.Normal); _lowSyncCtx = new CoreDispatcherSynchronizationContext(this, CoreDispatcherPriority.Low); _idleSyncCtx = new CoreDispatcherSynchronizationContext(this, CoreDispatcherPriority.Idle); _idleDispatchedHandlerArgs = new IdleDispatchedHandlerArgs(() => IsQueueIdle); Initialize(); }
private void DispatchItems() { UIAsyncOperation operation = null; CoreDispatcherPriority operationPriority = CoreDispatcherPriority.Normal; for (int i = 3; i >= 0; i--) { var queue = _queues[i]; lock (_gate) { if (queue.Count > 0) { operation = queue.Dequeue(); operationPriority = (CoreDispatcherPriority)(i - 2); if (DecrementGlobalCount() > 0) { EnqueueNative(); } break; } } } if (operation != null) { if (!operation.IsCancelled) { IDisposable runActivity = null; try { if (_trace.IsEnabled) { runActivity = _trace.WriteEventActivity( TraceProvider.CoreDispatcher_InvokeStart, TraceProvider.CoreDispatcher_InvokeStop, relatedActivity: operation.ScheduleEventActivity, payload: new[] { ((int)operationPriority).ToString(), operation.GetDiagnosticsName() } ); } using (runActivity) { using (CoreDispatcherSynchronizationContext.Apply(this, operationPriority)) { operation.Action(); operation.Complete(); } } } catch (Exception ex) { if (_trace.IsEnabled) { _trace.WriteEvent(TraceProvider.CoreDispatcher_Exception, EventOpcode.Send, new[] { ex.GetType().ToString(), operation.GetDiagnosticsName() }); } operation.SetError(ex); this.Log().Error("Dispatcher unhandled exception", ex); } } else { if (_trace.IsEnabled) { _trace.WriteEvent(TraceProvider.CoreDispatcher_Cancelled, EventOpcode.Send, new[] { operation.GetDiagnosticsName() }); } } } else { throw new InvalidOperationException("Dispatch queue is empty"); } }