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();
lock (imported) try
{ {
Interlocked.Increment(ref current); var model = await Import(path, notification.CancellationToken);
if (t.Exception == null) lock (imported)
{ {
imported.Add(t.Result); imported.Add(model);
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
{
Logger.Error(t.Exception, $@"Could not import ({Path.GetFileName(path)})");
}
} }
}, TaskContinuationOptions.NotOnCanceled))); catch (TaskCanceledException)
{
throw;
}
catch (Exception e)
{
Logger.Error(e, $@"Could not import ({Path.GetFileName(path)})");
}
finally
{
Interlocked.Increment(ref current);
}
}));
if (imported.Count == 0) if (imported.Count == 0)
{ {