Skip to content

Commit 0af0016

Browse files
committed
Improved MyLongRunningCommand.
1 parent 7ad4997 commit 0af0016

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/ProgressDialogEx/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
Margin="5,5,5,5"
7373
Click="ShowProgressDialogWithErrorAndCancelButtonAndProgress" />
7474
<Separator />
75-
<Button Content="ProgressDialog with task composition example"
75+
<Button Content="ProgressDialog with async task composition example"
7676
HorizontalContentAlignment="Left"
7777
Padding="5,0,5,0"
7878
Height="26"

src/ProgressDialogEx/MyLongRunningCommand.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using System.Windows;
45
using System.Windows.Input;
56
using ProgressDialogEx.ProgressDialog;
67

@@ -17,16 +18,18 @@ public MyLongRunningCommand(IProgressDialogService dialogService)
1718

1819
public void Execute(object parameter)
1920
{
20-
dialogService.Execute(DoWork, new ProgressDialogOptions { WindowTitle = "Loading files" });
21+
Task<int> task = dialogService.ExecuteAsync(DoWork, new ProgressDialogOptions { WindowTitle = "Loading files" });
22+
23+
MessageBox.Show(String.Format("Result = {0}", task.Result));
2124
}
2225

23-
void DoWork(CancellationToken cancellationToken, IProgress<string> progress)
26+
static async Task<int> DoWork(CancellationToken cancellationToken, IProgress<string> progress)
2427
{
25-
Task.Factory.StartNew(() => progress.Report("First"), cancellationToken)
26-
.ContinueWith(_ => Thread.Sleep(1000), cancellationToken)
27-
.ContinueWith(_ => progress.Report("Second"), cancellationToken)
28-
.ContinueWith(_ => Thread.Sleep(1000), cancellationToken)
29-
.Wait();
28+
return await Task.Factory.StartNew(() => progress.Report("First"), cancellationToken)
29+
.ContinueWith(_ => Thread.Sleep(1000), cancellationToken)
30+
.ContinueWith(_ => progress.Report("Second"), cancellationToken)
31+
.ContinueWith(_ => Thread.Sleep(1000), cancellationToken)
32+
.ContinueWith(_ => 42);
3033
}
3134

3235
public bool CanExecute(object parameter)

0 commit comments

Comments
 (0)