问题描述
我正在尝试在 Windows 10 通用应用程序上使用此代码下载文件:
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?
编辑 1:
任务的状态是等待激活",所以它不会抛出异常.它只是在等待,直到我重新启动手机才开始我一直在尝试使用相同的 url,我在电脑上没有这个问题.只和手机有关.该任务的属性如下:
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:
推荐答案
我终于找到了问题所在.当我开始下载操作并关闭应用程序而不取消操作时,BackgroundDownloader 会为下一次应用程序启动保留该操作.当下载操作的数量达到最大允许同时操作(我认为 5)时,下一个操作将在等待列表()上,直到前一个操作完成.所以当应用程序这样启动时,我不得不停止所有未完成的操作:
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);
}
});
这篇关于windows 10 应用程序下载操作未启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!