0

I got that downloader code running on Windows 10 IoT Core which tries to download a file to the applications local folder if it does not exist yet. If it does, it should return the path to that file.

Spoiler: The code snippet itself is working, see below.

    private async Task<string> GetAsync(string url)
    {
        url = WebUtility.UrlDecode(url);

        string fileName = Path.GetFileName(url);

        var folder = ApplicationData.Current.LocalFolder;
        var destinationFile = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

        if (new FileInfo(destinationFile.Path).Length > 0)
            return destinationFile.Path;

        var downloader = new BackgroundDownloader();
        var download = downloader.CreateDownload(new Uri(url), destinationFile);

        await download.StartAsync();

        return destinationFile.Path;
    }

Now, when the file does not exist, the code is running until line await download.StartAsync(). From there, it never returns. In the folder, an empty file with the given name is existent but only 0kb in size - that one's created implicitly while calling CreateFileAsync() before (and that's why I check for .Length to check the file existence).

Now comes the thing: That code is never returning, but if I kill the application (after enough time), the file gets written to disk, so the download apparantly succeeded but it did not "flush" to the file system. That does not happen if I do not kill the app, so refreshing the Windows Explorer view or looking for the file properties does not show any change in size.

I'm getting crazy on this, could somebody give me a hint? All the tips and hints from articles like those here did not work on the Pi:


Setup

  • Raspberry Pi 3
  • Windows 10 IoT Core 10.0.15063.0
  • Wired Ethernet connection
  • App capabilities (Package.appxmanifest)
    • Internet (Client & Server)
    • Internet (Client)
    • Private Networks (Client & Server)
    • Removable Storage
Waescher
  • 5,361
  • 3
  • 34
  • 51
  • Your code snippet runs in windows iot core headless app? – Rita Han Aug 03 '17 at 03:15
  • It does, yes. Sorry for the late reply. – Waescher Aug 03 '17 at 20:38
  • It works in headed app. If possible you can use headed app instead for a workaround. – Rita Han Aug 04 '17 at 01:14
  • Oh my god, I thought it was just me! Been struggling on this for days already. I can confirm that there were something wrong when running it on a headless app. It works fine in a UI app (x86,x6 and arm). Funny that the app was working last week, then suddenly it stops downloading (it will stay forever there, but when you stop the app, that is the only time it will dump the data to the file. All I can remember is that there was this windows update in the device early this week/last weekend. – s_o_user Aug 04 '17 at 07:40
  • Thanks @RitaHan-MSFT, when I set up a headed app, I cannot get my RFID scanner to work. The app just hangs at `SpiDevice.FromIdAsync(devices[0].Id, settings);` (from https://stackoverflow.com/a/34357827/704281) – Waescher Aug 04 '17 at 19:50

1 Answers1

1

This hanging can be caused by Async and WPF context locking. Try to use Task.Wait() instead of await. WPF UI locks awaits easily. I got my await SpiDevice.FromIdAsync(devs[0].Id, set) locked, and when I changed to devTask.Wait(), lock disappeared and device found from devTask.Result.

Patrick
  • 1,717
  • 7
  • 21
  • 28
Trainee
  • 41
  • 1
  • 4
  • Sounds fair, thanks for that. Unfortunately I cannot check it since we stopped that fun project in favor for others. One day I might come back to this. – Waescher Apr 30 '18 at 08:22