Reputation: 5737
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
Upvotes: 0
Views: 214
Reputation: 41
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
.
Upvotes: 1