2016-01-31 79 views
1

我不断收到输出到Debug.Window。我试图通过“appFolder.GetFilesAsync();”尝试读取文件列表时出现错误

Der Thread 0xd88 hat mit Code 0 (0x0) geendet. 
Der Thread 0x6fc hat mit Code 0 (0x0) geendet. 
Der Thread 0xce8 hat mit Code 0 (0x0) geendet. 
Der Thread 0x68c hat mit Code 0 (0x0) geendet. 

我的代码是:

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Threading; 
using System.Threading.Tasks; 
using Windows.Storage; 
using Windows.UI.Xaml.Controls; 

namespace WindowsIOTControlCenter.ImageRotation 
{ 
class ImageRotation 
{ 

    Timer _time; 
    IReadOnlyList<StorageFile> pictures; 

    public ImageRotation(Grid targetGrid) 
    { 
     pictures = scanImagesFolder().Result; 
    } 

    private async Task<IReadOnlyList<StorageFile>> scanImagesFolder() 
    { 
     try 
     { 
      StorageFolder appFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("ImageRotation\\BackgroundImages"); 

      IReadOnlyList<StorageFile> filesInFolder = await appFolder.GetFilesAsync(); 

      foreach (StorageFile file in filesInFolder) 
       Debug.WriteLine(file.Name + ", " + file.DateCreated); 
      return null; 
     } 
     catch(Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
      return null; 
     } 
    } 

} 
} 

我用了一个非常简单的例子,从INET,因为我原来的代码有我现在有这个例子同样的问题。

我基本上想要一个位于指定目录中的文件列表。

分步调试显示将目录分配给appFolder没有问题。

但是,当涉及到

IReadOnlyList<StorageFile> filesInFolder = await appFolder.GetFilesAsync(); 

上述输出滴出一步步骤。显然没有例外可以捕捉,否则它会继续捕捉(Exception ex)。

有没有人知道我做错了什么,或者可以指出我的问题的另一种解决方案?

任何提示是apreciated。

P.S .:对不起,我糟糕的英语。

回答

2

的翻译错误是 “线程0xd88代码为0(为0x0)结束”。这不是错误,该消息对于程序来说是正常的。

当你做pictures = scanImagesFolder().Result;你可能会导致你的程序死锁,通过调用.Result在任务使用await

你可以做几件事。

  1. 使用filesInFolder = await appFolder.GetFilesAsync().ConfigureAwait(false);,这使得它,所以它不再试图运行UI线程因此调用.Result在代码的其余部分是不太可能死锁。如果GetFilesAsync也不使用.ConfigureAwait(false)您仍可能发生死锁。
  2. 移动你的代码出了构造函数和你的方法使用await代替.Result

    class ImageRotation 
    { 
    
        Timer _time; 
        IReadOnlyList<StorageFile> pictures; 
    
        public ImageRotation(Grid targetGrid) 
        { 
        }; 
    
        public async Task LoadImages() 
        { 
         pictures = await scanImagesFolder(); 
        } 
    

    这将使pictures =在同样的情况下,如果你需要它。如果不需要该功能使用

    public async Task LoadImages() 
        { 
         pictures = await scanImagesFolder().ConfigureAwait(false); 
        } 
    

请阅读“Async/Await - Best Practices in Asynchronous Programming”,它会教你如不叫.Result和避免async void

的基本知识
0

尝试,而不是这样做: appFolder.GetFilesAsync()ConfigureAwait(假)

相关问题