|
@@ -1,6 +1,7 @@
|
|
|
using System;
|
|
|
using System.ComponentModel;
|
|
|
using System.Linq;
|
|
|
+using System.Threading;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
namespace InABox.WPF
|
|
@@ -51,16 +52,65 @@ namespace InABox.WPF
|
|
|
progress.ShowDialog();
|
|
|
}
|
|
|
|
|
|
+ private static void RunShowModal(ProgressForm progress, Action<IProgress<string>> work)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Shows progress dialog modally, with a cancel button.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="message"></param>
|
|
|
+ /// <param name="cancelButtonText"></param>
|
|
|
+ /// <param name="work"></param>
|
|
|
+ /// <returns>Whether the progress was completed without cancelling.</returns>
|
|
|
+ public static bool ShowModal(string message, string cancelButtonText, Action<IProgress<string>, CancellationToken> work)
|
|
|
+ {
|
|
|
+ var cancellationTokenSource = new CancellationTokenSource();
|
|
|
+
|
|
|
+ var progress = new ProgressForm(cancelButtonText)
|
|
|
+ {
|
|
|
+ DisplayImage = DisplayImage
|
|
|
+ };
|
|
|
+ progress.Progress.Content = message;
|
|
|
+
|
|
|
+ progress.Loaded += (_, args) =>
|
|
|
+ {
|
|
|
+ var worker = new BackgroundWorker();
|
|
|
+ var update = new Progress<string>(data => progress.Progress.Content = data);
|
|
|
+
|
|
|
+ progress.OnCancelled += () =>
|
|
|
+ {
|
|
|
+ cancellationTokenSource.Cancel();
|
|
|
+ progress.Close();
|
|
|
+ };
|
|
|
+
|
|
|
+ worker.DoWork += (o, e) => work(update, cancellationTokenSource.Token);
|
|
|
+ worker.RunWorkerCompleted +=
|
|
|
+ (o, e) => progress.Close();
|
|
|
+ worker.RunWorkerAsync();
|
|
|
+ };
|
|
|
+
|
|
|
+ progress.ShowDialog();
|
|
|
+ return !cancellationTokenSource.IsCancellationRequested;
|
|
|
+ }
|
|
|
+
|
|
|
public static void ShowModal(string message, Action<IProgress<string>> work)
|
|
|
{
|
|
|
- var progress = new ProgressForm();
|
|
|
- progress.DisplayImage = DisplayImage;
|
|
|
+ var progress = new ProgressForm
|
|
|
+ {
|
|
|
+ DisplayImage = DisplayImage
|
|
|
+ };
|
|
|
progress.Progress.Content = message;
|
|
|
progress.Loaded += (_, args) =>
|
|
|
{
|
|
|
var worker = new BackgroundWorker();
|
|
|
var update = new Progress<string>(data => progress.Progress.Content = data);
|
|
|
|
|
|
+ progress.OnCancelled += () =>
|
|
|
+ {
|
|
|
+ worker.CancelAsync();
|
|
|
+ };
|
|
|
+
|
|
|
worker.DoWork += (o, e) => work(update);
|
|
|
worker.RunWorkerCompleted +=
|
|
|
(o, e) => progress.Close();
|