public static IDisposable Push() { var inner = new TraceListener[Trace.Listeners.Count]; Trace.Listeners.CopyTo(inner, 0); Trace.Listeners.Clear(); var res = new NoDeadlockAssertListener(inner); Trace.Listeners.Add(res); return(res); }
/// <summary> /// Awaits the provided task on the UI thread. The function will be /// invoked on the UI thread to ensure the correct context is captured /// for any await statements within the task. /// </summary> /// <remarks> /// If called from the UI thread, the function is evaluated /// synchronously. /// </remarks> public override T InvokeTaskSync <T>(Func <Task <T> > func, CancellationToken cancellationToken) { return(_factory.RunAsync(async() => { // Convert assertions to exceptions while joining on a task // or the message box will deadlock. using (NoDeadlockAssertListener.Push()) { await _factory.SwitchToMainThreadAsync(cancellationToken); return await func(); } }).Join(cancellationToken)); }