2

由于某种原因,我无法弄清楚,我await在以下代码中的语句之外的代码永远不会到达。为什么?

public class FileSystemUpdateSource : IUpdateSource
{
    private ObservableCollection<UpdatePackage> _packages = new ObservableCollection<UpdatePackage>();
    private readonly DirectoryInfo _importDirectory = null;


    public FileSystemUpdateSource(DirectoryInfo a_importDirectory)
    {
        #region Argument Validation

        if (a_importDirectory == null)
            throw new ArgumentNullException("a_importDirectory");

        #endregion

        _importDirectory = a_importDirectory;

        Refresh();
    }

    public ObservableCollection<UpdatePackage> Packages
    {
        get { return _packages; }
    }

    public async void RefreshAsync()
    {
        var task = new Task<IEnumerable<UpdatePackage>>(CreatePackages);

        var packages = await task;

        // *** Code not reached ***

        Packages.Clear();
        Packages.AddRange(packages);

    }

    public void Refresh()
    {
        var packages = CreatePackages();

        Packages.Clear();
        Packages.AddRange(packages);
    }

    private IEnumerable<UpdatePackage> CreatePackages()
    {
        var packageFiles = from packageFile in _importDirectory.EnumerateFiles().AsParallel()
                           where FileIsZip(packageFile)
                           select new UpdatePackage(packageFile);

        return packageFiles;
    }

    private bool FileIsZip(FileInfo a_file)
    {
        using (var fin = a_file.OpenRead())
        {
            byte[] firstBytes = new byte[5];
            fin.Read(firstBytes, 0, 5);

            if (firstBytes[0] == 80 && firstBytes[1] == 75 && firstBytes[2] == 3 && firstBytes[3] == 4 && firstBytes[4] == 20)
                return true;
            else
                return false;
        }
    }
}


    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var importRoot = ApplicationSettings.Current.GetValue("ImportRoot") as System.IO.DirectoryInfo;

        IUpdateSource updateSource = new FileSystemUpdateSource(importRoot);

        updateSource.RefreshAsync();

        // *** Code IS reached here. ***
    }

编辑

答案要归功于task.Start()在类似之前添加到我的代码中await(感谢 Reed Copsey 的回答):

    /// <summary>
    /// Refresh the list of available update packages (asynchronously).
    /// </summary>
    public async Task RefreshAsync()
    {
        var task = new Task<IEnumerable<UpdatePackage>>(CreatePackages);
        task.Start();

        var packages = await task;

        Packages.Clear();
        Packages.AddRange(packages);
    }
4

1 回答 1

2

您正在Task错误地创建您的。

当您调用 时new Task,它不会启动任务。您应该Task.Run改用:

var task = Task.Run(CreatePackages);

这可以简化为:

public async void RefreshAsync()
{
    var packages = await Task.Run(CreatePackages);
于 2013-02-04T19:20:09.713 回答