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"); } }
private void DispatchItems() { UIAsyncOperation operation = null; var operationPriority = CoreDispatcherPriority.Normal; Rendering?.Invoke(null, RenderingEventArgsGenerator(DateTimeOffset.UtcNow - _startTime)); var didEnqueue = false; for (var 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) { didEnqueue = true; 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 (GetSyncContext(operationPriority).Apply()) { 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 if (!ShouldRaiseRenderEvents) { if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug)) { this.Log().Error("Dispatch queue is empty"); } } if (!didEnqueue && ShouldRaiseRenderEvents) { DispatchWakeUp(); } }