异步/等待方法和异常处理的最佳实践

时间:2016-01-06 15:25:28

标签: c# wpf asynchronous exception-handling async-await

我必须处理一些使用异步函数无法正常运行的旧东西,而且由于我对这个概念的理解有限,我很难找到处理以下问题的最佳方法。

我有一个按钮,单击该按钮将执行长时间运行的作业(解压缩大型ZIP存档,需要几分钟)。它的Command执行方法定义如下:

private async void Import()
{
    // some stuff
    tokenSource = new CancellationTokenSource();
    IProgress<ProgressReport> progress = new Progress<ProgressReport>(data => Report(data));

    try
    {
        await Task.Run(() =>
        {
            Backup(tokenSource.Token, progress);
            Unzip(tokenSource.Token, progress);
        });
    }
    catch(Exception)
    {
        // do some rollback operation
    }

等待定义的函数如下:

private void Backup(CancellationToken token, IProgress<ProgressReport> progress)
{
token.ThrowIfCancellationRequested();

    var parent = Path.GetFullPath(Path.Combine(Paths.DataDirectory, ".."));
    if (!Directory.Exists(parent))
    {
        progress.Report(new ProgressReport(Level.Info, string.Format(
                "Root Directory ({0}) does not exist; Creating it.", parent)));
        Directory.CreateDirectory(parent);
        return;
    }

    if (!Directory.Exists(Paths.DataDirectory))
    {
        progress.Report(new ProgressReport(Level.Info, string.Format(
                "Data Directory ({0}) does not exist; nothing to backup.", Paths.DataDirectory)));
        return;
    }

    // Generate a name for the backup           
    try
    {
        progress.Report(new ProgressReport(Level.Info, string.Format(
                "Renaming source Data Directory ({0}) to a temporary name.", Paths.DataDirectory)));

        var temp = Path.Combine(parent, Guid.NewGuid().ToString("N"));
        Directory.Move(Paths.DataDirectory, temp);
        // Success, let's store the backupFolder in a class field
        backupFolder = temp;

        progress.Report(new ProgressReport(Level.Info, string.Format(
                "Source Data Directory ({0}) successfully renamed to {1}.", Paths.DataDirectory, backupFolder)));

        token.ThrowIfCancellationRequested();
    }
    catch (OperationCanceledException)
    {
        progress.Report(new ProgressReport(Level.Warn, "Cancelling Import Operation."));
        throw;
    }
    catch (Exception ex)
    {
        // some stuff then throw Exception to bubble-up
        throw;
    }
}

private async Task Unzip(CancellationToken token, IProgress<ProgressReport> progress)
{
    try
    {
        token.ThrowIfCancellationRequested();

        var parent = Path.GetFullPath(Path.Combine(Paths.DataDirectory, ".."));

        try
        {
            progress.Report(new ProgressReport(
                Level.Info, string.Format("Uncompressing export file {0}.", InputFileName)));

            using (var zipArchive = ZipFile.Open(InputFileName, ZipArchiveMode.Read, Encoding.UTF8))
            {
                var extractedCount = 0;
                var totalCount = zipArchive.Entries.Count;
                foreach (var entry in zipArchive.Entries)
                {
                    progress.Report(new ProgressReport(
                        Level.Off, string.Format("Extracting {0}, {1}/{2}",
                        entry.FullName, extractedCount + 1, totalCount), extractedCount, totalCount));

                    if (string.IsNullOrEmpty(entry.Name) && string.IsNullOrEmpty(entry.FullName))
                        continue;

                    if (string.IsNullOrEmpty(entry.Name))
                    {
                        var dir = Path.Combine(parent, entry.FullName);
                        if (!Directory.Exists(dir))
                            Directory.CreateDirectory(dir);
                    }
                    else entry.ExtractToFile(Path.Combine(parent, entry.FullName), true);

                    notPaused.WaitOne();
                    token.ThrowIfCancellationRequested();

                    extractedCount++;
                }

                // Everything completed, update the progress bar.
                progress.Report(new ProgressReport(totalCount, totalCount));
            }
        }
        catch (OperationCanceledException)
        {
            progress.Report(new ProgressReport(Level.Warn, "Cancelling Import Operation."));
            throw;
        }
        catch (Exception ex)
        {
            // Some stuff then throw Exception to bubble-up
            throw;
        }
    }
}

此时,异步作业工作正常,UI未被冻结,但问题是ExceptionBackup方法中引发的Unzip从未冒泡并且不是&# 39;用Import方法捕获,因此程序在throw指令处崩溃。

在做了一些研究之后,我在this msdn article中发现这是使用void返回方法时的正常行为。所以我稍微更改了程序,现在await的调用是这样的:

try
{
    await Backup(tokenSource.Token, progress);
    await Unzip(tokenSource.Token, progress);
}

我的方法定义如下:

private async Task Backup(CancellationToken token, IProgress<ProgressReport> progress)
{
    // Same logic
    Task.Delay(1000);
}

private async Task Unzip(CancellationToken token, IProgress<ProgressReport> progress)
{
    // Same logic
    Task.Delay(1000);
}

现在异常很快就会向Import方法冒泡,但UI会在整个作业完成时间内被冻结,就像UI线程处理作业一样。什么错误的暗示?

2 个答案:

答案 0 :(得分:-1)

我建议您按照以下方式设置async-await

try
{
    await Backup(tokenSource.Token, progress);
    await Unzip(tokenSource.Token, progress);
}
catch (Exception ex)
{
    // your exceptions from the async tasks will end up here
}

这样定义的方法如下:

private async Task Backup(CancellationToken token, IProgress<ProgressReport> progress)
{
    await Task.Run(() =>
    {
        // do cpu bound work here (async)
    });
}

private async Task Unzip(CancellationToken token, IProgress<ProgressReport> progress)
{
    await Task.Run(() =>
    {
        // do cpu bound work here (async)
    });
}

现在,例外总是会在主要的try {} catch

中结束

有关async-await的更多信息,请随时咨询Stephen Clary的博客:http://blog.stephencleary.com/2012/02/async-and-await.html

答案 1 :(得分:-2)

感谢@ i3arnon评论,我设法实现了我想要的。 这是我的电话改变的方式:

await Task.Run(async () =>
{
    await Backup(tokenSource.Token, progress);
    await Unzip(tokenSource.Token, progress);
});

这两个函数仍然被声明为private async Task

这样,由于Task.Run,作业正在后台运行,因此UI不会冻结,异常会正常冒泡。