@@ -62,14 +62,66 @@ public bool TryExecute<T>(Func<IProgress<string>, T> action, ProgressDialogOptio
62
62
isCancellable : false ) ;
63
63
}
64
64
65
- public bool TryExecute < T > ( Func < CancellationToken , IProgress < string > , T > action ,
65
+ public bool TryExecute < T > ( Func < CancellationToken , IProgress < string > , T > action ,
66
66
ProgressDialogOptions options , out T result )
67
67
{
68
68
if ( action == null ) throw new ArgumentNullException ( "action" ) ;
69
69
return TryExecuteInternal ( action , options , out result ) ;
70
70
}
71
71
72
- private void ExecuteInternal ( Action < CancellationToken , IProgress < string > > action ,
72
+ public async Task ExecuteAsync ( Func < Task > action , ProgressDialogOptions options )
73
+ {
74
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
75
+ await ExecuteAsyncInternal ( ( token , progress ) => action ( ) , options ) ;
76
+ }
77
+
78
+ public async Task ExecuteAsync ( Func < CancellationToken , Task > action , ProgressDialogOptions options )
79
+ {
80
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
81
+ await ExecuteAsyncInternal ( ( token , progress ) => action ( token ) , options ) ;
82
+ }
83
+
84
+ public async Task ExecuteAsync ( Func < IProgress < string > , Task > action , ProgressDialogOptions options )
85
+ {
86
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
87
+ await ExecuteAsyncInternal ( ( token , progress ) => action ( progress ) , options ,
88
+ isCancellable : false ) ;
89
+ }
90
+
91
+ public async Task ExecuteAsync ( Func < CancellationToken , IProgress < string > , Task > action ,
92
+ ProgressDialogOptions options )
93
+ {
94
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
95
+ await ExecuteAsyncInternal ( action , options ) ;
96
+ }
97
+
98
+ public async Task < T > ExecuteAsync < T > ( Func < Task < T > > action , ProgressDialogOptions options )
99
+ {
100
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
101
+ return await ExecuteAsyncInternal ( ( token , progress ) => action ( ) , options ) ;
102
+ }
103
+
104
+ public async Task < T > ExecuteAsync < T > ( Func < CancellationToken , Task < T > > action , ProgressDialogOptions options )
105
+ {
106
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
107
+ return await ExecuteAsyncInternal ( ( token , progress ) => action ( token ) , options ) ;
108
+ }
109
+
110
+ public async Task < T > ExecuteAsync < T > ( Func < IProgress < string > , Task < T > > action , ProgressDialogOptions options )
111
+ {
112
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
113
+ return await ExecuteAsyncInternal ( ( token , progress ) => action ( progress ) , options ,
114
+ isCancellable : false ) ;
115
+ }
116
+
117
+ public async Task < T > ExecuteAsync < T > ( Func < CancellationToken , IProgress < string > , Task < T > > action ,
118
+ ProgressDialogOptions options )
119
+ {
120
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
121
+ return await ExecuteAsyncInternal ( action , options ) ;
122
+ }
123
+
124
+ private void ExecuteInternal ( Action < CancellationToken , IProgress < string > > action ,
73
125
ProgressDialogOptions options , bool isCancellable = true )
74
126
{
75
127
if ( action == null ) throw new ArgumentNullException ( "action" ) ;
@@ -100,7 +152,7 @@ private void ExecuteInternal(Action<CancellationToken, IProgress<string>> action
100
152
}
101
153
102
154
private bool TryExecuteInternal < T > (
103
- Func < CancellationToken , IProgress < string > , T > action ,
155
+ Func < CancellationToken , IProgress < string > , T > action ,
104
156
ProgressDialogOptions options , out T result , bool isCancellable = true )
105
157
{
106
158
if ( action == null ) throw new ArgumentNullException ( "action" ) ;
@@ -111,7 +163,7 @@ private bool TryExecuteInternal<T>(
111
163
CancellationToken cancellationToken = cancellationTokenSource . Token ;
112
164
113
165
var cancelCommand = isCancellable ? new CancelCommand ( cancellationTokenSource ) : null ;
114
-
166
+
115
167
var viewModel = new ProgressDialogWindowViewModel (
116
168
options , cancellationToken , cancelCommand ) ;
117
169
@@ -144,5 +196,67 @@ private bool TryExecuteInternal<T>(
144
196
return false ;
145
197
}
146
198
}
199
+
200
+ async private Task < T > ExecuteAsyncInternal < T > (
201
+ Func < CancellationToken , IProgress < string > , Task < T > > action ,
202
+ ProgressDialogOptions options , bool isCancellable = true )
203
+ {
204
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
205
+ if ( options == null ) throw new ArgumentNullException ( "options" ) ;
206
+
207
+ using ( var cancellationTokenSource = new CancellationTokenSource ( ) )
208
+ {
209
+ CancellationToken cancellationToken = cancellationTokenSource . Token ;
210
+
211
+ var cancelCommand = isCancellable ? new CancelCommand ( cancellationTokenSource ) : null ;
212
+
213
+ var viewModel = new ProgressDialogWindowViewModel (
214
+ options , cancellationToken , cancelCommand ) ;
215
+
216
+ var window = new ProgressDialogWindow
217
+ {
218
+ DataContext = viewModel
219
+ } ;
220
+
221
+ Task < T > task = action ( cancellationToken , viewModel . Progress ) ;
222
+
223
+ task . ContinueWith ( _ => viewModel . Close = true ) ;
224
+
225
+ window . ShowDialog ( ) ;
226
+
227
+ return await task ;
228
+ }
229
+ }
230
+
231
+ async private Task ExecuteAsyncInternal (
232
+ Func < CancellationToken , IProgress < string > , Task > action ,
233
+ ProgressDialogOptions options , bool isCancellable = true )
234
+ {
235
+ if ( action == null ) throw new ArgumentNullException ( "action" ) ;
236
+ if ( options == null ) throw new ArgumentNullException ( "options" ) ;
237
+
238
+ using ( var cancellationTokenSource = new CancellationTokenSource ( ) )
239
+ {
240
+ CancellationToken cancellationToken = cancellationTokenSource . Token ;
241
+
242
+ var cancelCommand = isCancellable ? new CancelCommand ( cancellationTokenSource ) : null ;
243
+
244
+ var viewModel = new ProgressDialogWindowViewModel (
245
+ options , cancellationToken , cancelCommand ) ;
246
+
247
+ var window = new ProgressDialogWindow
248
+ {
249
+ DataContext = viewModel
250
+ } ;
251
+
252
+ Task task = action ( cancellationToken , viewModel . Progress ) ;
253
+
254
+ task . ContinueWith ( _ => viewModel . Close = true ) ;
255
+
256
+ window . ShowDialog ( ) ;
257
+
258
+ await task ;
259
+ }
260
+ }
147
261
}
148
262
}
0 commit comments