GetFileSASYNC停止工作



我有这个代码

public static class Storage
{
    public async static Task<bool> Exists(string filename)
    {
        var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets");
        var _files= await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);
        var file = _files.FirstOrDefault(x => x.Name == filename);
        return file != null;
    }
}

并从我的Windows 8 Store应用程序调用;

this.IconExists = this.Game != null && Storage.Exists(this.IconName).Result;

因此,如果我在上面的行上放置一个断点并逐步运行它,它可以正常工作,但没有破坏并仅运行该应用程序会导致挂在应用程序中。

几天前也有类似的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Storage.Search;
namespace eggrr.Core.FileStorage
{
    public class Storage
    {
        private IReadOnlyList<StorageFile> _files;
        public Storage()
        {
            _files = GetFilesAsync("Assets").Result;
        }
        private async Task<IReadOnlyList<StorageFile>> GetFilesAsync(string relativeFolderPath)
        {
            var path = string.Format("{0}\{1}", Package.Current.InstalledLocation.Path, relativeFolderPath);
            var folder = await StorageFolder.GetFolderFromPathAsync(path);
            return await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);
        }
        public bool Exists(string filename)
        {
            var file = _files.FirstOrDefault(x => x.Name == filename);
            return file != null;
        }
        private static readonly Storage _instance = new Storage();
        public static Storage Instance { get { return _instance; } }
    }
}

有什么想法?

似乎解决了问题;

    public static class Storage
{
    private static IReadOnlyList<StorageFile> _files;
    static Storage()
    {
        _files = GetFilesAsync("Assets").Result;
    }
    private async static Task<IReadOnlyList<StorageFile>> GetFilesAsync(string relativeFolderPath)
    {
        var folder = await Package.Current.InstalledLocation.GetFolderAsync("Assets").AsTask().ConfigureAwait(false);
        return await folder.GetFilesAsync(CommonFileQuery.OrderByName).AsTask().ConfigureAwait(false);
    }
    public static bool Exists(string filename)
    {
        var file = _files.FirstOrDefault(x => x.Name == filename);
        return file != null;
    }
}

有关;

的更多信息
  • winrt:使用getFileFromApplicationUriaSync()
  • 加载静态数据
  • http://nitprograms.blogspot.com/2012/07/dont-block-on-async-code.html
  • http://lunarfrog.com/blog/2012/01/23/simplicity-of-async-and-await/

最新更新