Avoid using ContinueWith in already async context

This commit is contained in:
Dean Herbert 2019-06-10 17:12:25 +09:00
parent 5b75060b94
commit 559413f766
1 changed files with 19 additions and 10 deletions

View File

@ -152,26 +152,35 @@ protected async Task Import(ProgressNotification notification, params string[] p
var imported = new List<TModel>(); var imported = new List<TModel>();
await Task.WhenAll(paths.Select(path => Import(path, notification.CancellationToken).ContinueWith(t => await Task.WhenAll(paths.Select(async path =>
{ {
notification.CancellationToken.ThrowIfCancellationRequested(); notification.CancellationToken.ThrowIfCancellationRequested();
try
{
var model = await Import(path, notification.CancellationToken);
lock (imported) lock (imported)
{ {
Interlocked.Increment(ref current); imported.Add(model);
if (t.Exception == null)
{
imported.Add(t.Result);
notification.Text = $"Imported {current} of {paths.Length} {term}s"; notification.Text = $"Imported {current} of {paths.Length} {term}s";
notification.Progress = (float)current / paths.Length; notification.Progress = (float)current / paths.Length;
} }
else }
catch (TaskCanceledException)
{ {
Logger.Error(t.Exception, $@"Could not import ({Path.GetFileName(path)})"); throw;
} }
catch (Exception e)
{
Logger.Error(e, $@"Could not import ({Path.GetFileName(path)})");
} }
}, TaskContinuationOptions.NotOnCanceled))); finally
{
Interlocked.Increment(ref current);
}
}));
if (imported.Count == 0) if (imported.Count == 0)
{ {