Example #1
0
        public override WaitDialogResult <TResult> TryCreateWaitDialog <TResult>(string title, string message, Func <WaitDialogContext, Task <TResult> > onWaitAsync)
        {
            Debug.Assert(_joinableTaskFactory.Context.IsOnMainThread);

            var result = _waitDialogFactory.CreateInstance(out var dialog2);

            if (result != VSConstants.S_OK)
            {
                return(null);
            }

            if (dialog2 is not IVsThreadedWaitDialog4 dialog4)
            {
                Debug.Fail("This is unexpected, the dialog should always be an IVsThreadedWaitDialog4");
                return(null);
            }

            var context  = new DefaultWaitDialogContext();
            var callback = new Callback(context);

            dialog4.StartWaitDialogWithCallback(title, message, szProgressText: null, varStatusBmpAnim: null, szStatusBarText: null, fIsCancelable: true, iDelayToShowDialog: 2, fShowProgress: false, iTotalSteps: 0, iCurrentStep: 0, pCallback: callback);

            TResult waitResult = default;

            _joinableTaskFactory.Run(async() =>
            {
                try
                {
                    waitResult = await onWaitAsync(context).ConfigureAwait(false);
                }
                catch (TaskCanceledException)
                {
                    // Swallow task cancelled exceptions, they represent the user cancelling the wait dialog.
                }
            });

            dialog4.EndWaitDialog(out var cancelledResult);

            var cancelled    = cancelledResult != 0;
            var dialogResult = new WaitDialogResult <TResult>(waitResult, cancelled);

            return(dialogResult);
        }
Example #2
0
 public Callback(DefaultWaitDialogContext waitContext) => _waitContext = waitContext;