Reputation: 111
I'm trying to download a file using this code on a windows 10 universal app:
await downloadOperation.StartAsync().AsTask(token, progressCallback);
it's working on pc but on mobile sometimes it's doesn't start downloading and not even giving an exception until I restart the mobile. Is it a bug in the system or I'm missing something?
Edit 1:
the task's status is "waiting for activation" so it's not throwing an exception.
it's just waiting and not starting until I restart the phone
I'm trying always with the same url and I don't have this problem on the pc. It's about the phone only.
The task's properties are the following:
Upvotes: 6
Views: 1160
Reputation: 111
I found the problem finally. when I start a download operation and close the application without cancelling the operation the BackgroundDownloader keeps the operation for the next application start. when the number of download operations reach the maximum allowed simultaneous operations(I think 5) the next operations will be on the waiting list() till the previous operations finish. so I had to stop all the uncompleted operations when the application starts like this:
Task.Run(async () =>
{
var downloads = await BackgroundDownloader.GetCurrentDownloadsAsync();
foreach (var download in downloads)
{
CancellationTokenSource cts = new CancellationTokenSource();
download.AttachAsync().AsTask(cts.Token);
cts.Cancel();
}
var localFolder = ApplicationData.Current.LocalFolder;
var files = await localFolder.GetFilesAsync();
files = files.Where(x => x.Name.EndsWith("_")).ToList();
foreach (StorageFile file in files)
{
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
});
Upvotes: 3