windows 10 apps DownloadOperation not starting

别来无恙 提交于 2019-11-30 14:20:47

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