Morigal
Morigal

Reputation: 13

I keep getting errors while trying to read a list of files via "appFolder.GetFilesAsync();"

I keep getting following Output to Debug.Window.

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.

My code is:

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

}
}

I used a very basic Example from inet because my original code had the same problem I have now with this example.

I basically want a list of files, located in the specified directory.

Step-by-Step debugging shows no problems assigning the directory to appFolder.

But when it comes to

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

the output mentioned above drops out step-by-step. There's obviously no Exception to catch, otherwise it would continue with catch(Exception ex).

Has anyone a idea what I'm doing wrong or can point out another solution to my problem?

Any hints are apreciated.

P.S.: Sorry for my lousy english.

Upvotes: 1

Views: 82

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127543

The translation for the error is "The thread 0xd88 ended with code 0 (0x0)". This is not a error, this message is normal for the program to get.

When you do pictures = scanImagesFolder().Result; you are likely causing a deadlock on your program by calling .Result on a task that uses await.

A few things you could do.

  1. Use filesInFolder = await appFolder.GetFilesAsync().ConfigureAwait(false);, this makes it so it no longer tries to run the rest of the code on the UI thread so calling .Result is less likely to deadlock. If GetFilesAsync does not also use .ConfigureAwait(false) you could still deadlock.
  2. Move your code out of the constructor and use await on your method instead of .Result

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

    This will make the pictures = on the same context if you need it. If you don't need that functionality use

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

Please read "Async/Await - Best Practices in Asynchronous Programming", it will teach you the basics like not calling .Result and avoiding async void

Upvotes: 2

Newbie
Newbie

Reputation: 187

Try doing this instead: appFolder.GetFilesAsync().ConfigureAwait(false)

Upvotes: 0

Related Questions