GetFilesAsync停止工作 [英] GetFilesAsync stops working

查看:187
本文介绍了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应用程序调用它;

and calling it from my Windows 8 Store application;

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



所以,如果我把一个破发点以上线并运行它一步一步来,它的工作原理, 。但没有打破,只是运行的应用程序导致挂起的应用程序

So if I put a break point on the above line and run it step by step, it works, but without breaking and just running the application causes a hang in application.

和类似的代码正在对前几天的承诺;

And a similar code was working on a few days ago's commit;

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; } }
    }
}



任何想法?

Any ideas?

推荐答案

看来这个解决它的问题;

Seems this solved it issue;

    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: Loading static data with GetFileFromApplicationUriAsync()
  • http://nitoprograms.blogspot.com/2012/07/dont-block-on-async-code.html
  • http://lunarfrog.com/blog/2012/01/23/simplicity-of-async-and-await/

这篇关于GetFilesAsync停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆