public Task ShowAsync() { return(AsyncHelper.InvokeAsync <EventHandler>( Show, h => _window.Closed += h, h => _window.Closed -= h, tcs => (s, e) => tcs.TrySetResult(true))); }
private async Task CalibrateAsync(ICalibrator calibrator) { var execution = calibrator as IEyeTrackerCalibrationExecution; var result = SessionStepResult.Failed; using (var cancellation = new CancellationDisposable()) { _cancellation = cancellation; try { execution.CalibrationFinished += Execution_CalibrationFinished; await AsyncHelper.InvokeAsync <CalibratorStateChangedEventHandler, bool> ( () => { }, h => calibrator.StateChanged += h, h => calibrator.StateChanged -= h, tcs => (c, state) => { switch (state) { case CalibratorState.Canceled: tcs.TrySetResult(false); break; case CalibratorState.Completed: tcs.TrySetResult(true); break; } }, cancellation.Token ); // create result with calibrations result = new EyeTrackerCalibrationSessionStepResult(_calibrations); } catch (OperationCanceledException) { await calibrator.CancelAsync(); } finally { //remove handler from execution if (execution != null) { execution.CalibrationFinished -= Execution_CalibrationFinished; } } Navigation.Clear(); _cancellation = null; } OnCompleted(result); }
public static async Task WaitUntilLoadedAsync(this FrameworkElement element) { await AsyncHelper.InvokeAsync <RoutedEventHandler> ( () => { }, handler => element.Loaded += handler, handler => element.Loaded -= handler, tcs => (o, args) => tcs.SetResult(true) ); }
public Task ShowAsync() { var flyout = Flyout; flyout.Position = (Position)Position; return(AsyncHelper.InvokeAsync <RoutedEventHandler>( Show, h => flyout.ClosingFinished += h, h => flyout.ClosingFinished -= h, tcs => (s, e) => tcs.TrySetResult(true))); }
private async Task TryStartCalibrationAsync(ICalibrate calibrate) { if (CanCalibrate == false) { using (var cancellation = new CancellationDisposable()) { _cancellation = cancellation; await AsyncHelper.InvokeAsync <EventHandler <bool>, bool> ( () => { }, h => calibrate.CanCalibrateChanged += h, h => calibrate.CanCalibrateChanged -= h, tcs => (_, canCalibrate) => { if (canCalibrate) { tcs.TrySetResult(canCalibrate); } }, cancellation.Token ); OnPropertyChanged(nameof(CanCalibrate)); _cancellation = null; } } if (CanCalibrate) { var calibrator = calibrate.GetCalibrator(); var calibratorViewModel = (CalibratorViewModel)_resolver.Create(calibrator); ApplySettings(calibratorViewModel); Navigation.NavigateToObject(calibratorViewModel); await CalibrateAsync(calibrator); } else { Complete(); } }
public static async Task RunAsync(this Storyboard storyboard, CancellationToken cancellationToken) { try { await AsyncHelper.InvokeAsync <EventHandler>( () => storyboard.Begin(), handler => storyboard.Completed += handler, handler => storyboard.Completed -= handler, tcs => (s, e) => tcs.TrySetResult(true), cancellationToken); } catch (OperationCanceledException) { storyboard.Stop(); throw; } }
public async Task SendAsync(IdentityMessage message) { // Plug in your email service here to send an email. string text = String.IsNullOrWhiteSpace(message.Body) ? message.Subject : String.Format("Please click on this link to {0}: {1}", message.Subject.ToLower(), message.Body); string html = String.IsNullOrWhiteSpace(message.Body) ? message.Subject : String.Format("Please click on this link to {0}: <a href=\"{1}\">link</a><br/><br/>Or copy and open the following link in the browser:<br/>{1}", message.Subject.ToLower(), message.Body); using (var client = new SmtpClient()) using (var mail = new MailMessage()) { mail.To.Add(new MailAddress(message.Destination)); mail.Subject = "[UXR] " + message.Subject; mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain)); mail.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html)); await AsyncHelper.InvokeAsync <SendCompletedEventHandler> ( () => client.SendAsync(mail, null), h => client.SendCompleted += h, h => client.SendCompleted -= h, (tcs) => (s, e) => { if (e.Cancelled) { tcs.TrySetCanceled(); } else if (e.Error != null) { tcs.TrySetException(e.Error); } else { tcs.TrySetResult(true); } } ); } }
/// <summary> /// /// </summary> /// <returns></returns> /// <remarks>See Tobii docs p. 20.</remarks> private async Task RunCalibrationAsync() { bool cancelled = false; bool failed = false; var calibration = _calibration; using (CancellationTokenSource cts = new CancellationTokenSource()) { KeyEventHandler keyCancelEventHandler = CreateKeyCancelEventHandler(cts); CancelEventHandler cancelEventHandler = CreateCancelEventHandler(cts); try { if (_window != null) { _window.KeyDown += keyCancelEventHandler; _window.Closing += cancelEventHandler; } VisualStateManager.GoToElementState((FrameworkElement)this.Content, nameof(IntroductionState), true); await Task.Delay(3000, cts.Token); VisualStateManager.GoToElementState((FrameworkElement)this.Content, nameof(CalibrationProcessState), true); await Task.Delay(300, cts.Token); var failedTask = AsyncHelper.InvokeAsync <EventHandler <Exception> > ( () => { }, h => calibration.Failed += h, h => calibration.Failed -= h, tcs => (_, ex) => tcs.TrySetException(ex), cts.Token ); var completedTask = AsyncHelper.InvokeAsync <EventHandler <CalibrationExecutionReport> > ( () => calibration.Start(), h => calibration.Completed += h, h => calibration.Completed -= h, tcs => (_, __) => tcs.TrySetResult(true), cts.Token ); var finishedTask = await Task.WhenAny(failedTask, completedTask); if (finishedTask.IsCanceled) { cancelled = true; } else if (finishedTask == failedTask) { failed = true; // TODO 10/09/2016 add error // show message based on the exception } // TODO 12/10/2016 Show loading if the evaluation takes too long. // await calibration.FinishAsync(); } catch (OperationCanceledException) { cancelled = true; } catch (Exception) { failed = true; // Show unknown error } finally { if (_window != null) { _window.KeyDown -= keyCancelEventHandler; _window.Closing -= cancelEventHandler; } } } if (cancelled) { calibration?.Cancel(); } else if (failed) { VisualStateManager.GoToElementState((FrameworkElement)this.Content, nameof(ErrorState), true); } }