1

I'm pretty new in developing UWP apps and using await/async. I try getting a file from local folder of UWP app, but the app is hanging. The file exists on drive.

I wrote some test methods to try identifying the issue, but I don't understand, why it doesn't work. Two runs working fine and the third hangs always.

Has someone an idea, what I'm doing woring?

Code:

    public void Test()
    {
        for (var i = 0; i < 10; i++)
        {
            KLogger.Log.Debug("");
            KLogger.Log.Debug("### Started : " + i);

            // create new or returns existent file
            var task = TestGetNewFile("test.txt");
            //task.ConfigureAwait(false);
            task.Wait();
            var res = task.Result;

            task.Dispose();

            KLogger.Log.Debug("### Done : " + i);
        }
    }

    public async Task<IStorageFile> TestGetNewFile(string fileName)
    {
        var storageFolder = ApplicationData.Current.LocalFolder;

        KLogger.Log.Debug("Creation started.");

        var fileItem = await storageFolder.TryGetItemAsync(fileName);

        KLogger.Log.Debug("Creation finished.");

        return null;
    }

Log Output:

2018-08-12 00:41:07.282 +02:00 [DBG] 
2018-08-12 00:41:07.282 +02:00 [DBG] ### Started : 0
2018-08-12 00:41:07.284 +02:00 [DBG] Creation started.
2018-08-12 00:41:07.289 +02:00 [DBG] Creation finished.
2018-08-12 00:41:07.289 +02:00 [DBG] ### Done : 0
2018-08-12 00:41:07.289 +02:00 [DBG] 
2018-08-12 00:41:07.289 +02:00 [DBG] ### Started : 1
2018-08-12 00:41:07.289 +02:00 [DBG] Creation started.
2018-08-12 00:41:07.300 +02:00 [DBG] Creation finished.
2018-08-12 00:41:07.300 +02:00 [DBG] ### Done : 1
2018-08-12 00:41:07.300 +02:00 [DBG] 
2018-08-12 00:41:07.300 +02:00 [DBG] ### Started : 2
2018-08-12 00:41:07.300 +02:00 [DBG] Creation started.
-- nothing more in log file --
Isgam
  • 35
  • 4
  • Your code does not compile. There is no Dispose method in Task class. – kennyzx Aug 12 '18 at 03:50
  • // Assembly location: C:\Users\user\.nuget\packages\microsoft.netcore.universalwindowsplatform\6.1.5\ref\uap10.0.15138\System.Runtime.dll - Task has a Dispose method. But you can remove the Dispose call. This was a test only. Without dispose, issue is the same.The UWP app is for target platform Windows 10, Version 1803 (10.0; Build 17134) Min. Version Windows 10 Fall Creators Update (10.0; Build 16xxx) – Isgam Aug 12 '18 at 07:30
  • If you're calling this code from the UI thread, then it's surprising that it works the first two times, this is a known deadlock pattern. Try instead to call your method like this: `var task = Task.Run(() => TestGetNewFile("test.txt"));` – Kevin Gosse Aug 12 '18 at 08:38

1 Answers1

2

It works if you call it this way, use async-await all the way down.

private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        var item = await TestGetNewFile("test.txt"); 
    }
}

async Task<IStorageItem> TestGetNewFile(string fileName)
{
     return await ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName);         
}

You may find this answer await vs. Task.Wait - Deadlock? useful.

kennyzx
  • 12,845
  • 6
  • 39
  • 83
  • Hey! Great! This works perfect now with "async-await all the way down". Thanks a lot for your quick help! – Isgam Aug 12 '18 at 23:17