• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

GetFilesAsync停止工作

用户头像
it1352
帮助1

问题说明

我有这段代码

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?

正确答案

#1

看来这个解决它的问题;

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/

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcffiff
系列文章
更多 icon
同类精品
更多 icon
继续加载