コード例 #1
0
        public static async Task WaitWithProgress(
            this Task task, string caption, CancellationTokenSource cts = null)
        {
            var dialogCts = new CancellationTokenSource();

            var dialog = new TaskDialog {
                Caption = caption,
                Content = string.Empty
            };

            if (cts != null)
            {
                dialog.IsCancelable  = true;
                dialog.CommonButtons = TaskDialogButtons.Cancel;
            }
            dialog.Controls.Add(new TaskDialogProgressBar {
                IsIndeterminate = true
            });

            dialog.Closing += (s, e) => {
                if (e.Button == TaskDialogButtonId.Cancel)
                {
                    dialogCts.Cancel();
                    cts?.Cancel();
                }
            };

            await Task.WhenAny(task, dialog.ShowModalDelayed(dialogCts.Token));

            dialogCts.Cancel();
            if (dialog.IsShown)
            {
                dialog.Abort();
            }
        }