windows 10 apps DownloadOperation not starting

狂风中的少年 提交于 2019-11-30 15:26:36

问题


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:


回答1:


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


来源:https://stackoverflow.com/questions/31463770/windows-10-apps-downloadoperation-not-starting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!