我想为 BackgroundDownloader 实现超时功能。当我超时时,我无法取消下载操作。所以我这样使用它:
public async void downloadFile(string fileUrl, string fileName) {
var myFolder = await StorageFolder.GetFolderFromPathAsync(Package.Current.InstalledLocation.Path);
var myFile = await myFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
var downloader = new BackgroundDownloader();
var downloadOperation = downloader.CreateDownload(new Uri(fileUrl), myFile);
var task = Task.Run(async () => await downloadOperation.StartAsync().AsTask());
if ( task.Wait(TimeSpan.FromMilliseconds(1000)) ) {
// file is downloaded in time
} else {
// timeout is reached - how to cancel downloadOperation ?????
}
}
我在尝试:
downloadOperation.StartAsync().Cancel();
我明白了
WinRT information: This operation was already started. Call AttachAsync to attach to a running download/upload.
downloadOperation.AttachAsync().Cancel();
我明白了
Exception thrown: 'System.Runtime.InteropServices.COMException' in Project.exe WinRT information: This operation was not started. Call StartAsync to start the operation.Additional information: A method was called at an unexpected time.
我们将不胜感激任何想法!
最佳答案
When I reach timeout i can't cancel download operation. I was trying : downloadOperation.AttachAsync().Cancel();
根据我的测试,downloadOperation.AttachAsync().Cancel();
在我的网站上运行良好。以下是我用于测试的代码:
public async void downloadFile(string fileUrl, string fileName)
{
var myFolder = await StorageFolder.GetFolderFromPathAsync(Package.Current.InstalledLocation.Path);
var myFile = await myFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
var downloader = new BackgroundDownloader();
var downloadOperation = downloader.CreateDownload(new Uri(fileUrl), myFile);
var task = Task.Run(async () => await downloadOperation.StartAsync().AsTask());
if (task.Wait(TimeSpan.FromMilliseconds(1000)))
{
// file is downloaded in time
}
else {
// timeout is reached - how to cancel downloadOperation ?????
downloadOperation.AttachAsync().Cancel();
}
}
通常我们使用CancellationToken取消下载操作。此外,使用 Task.Wait方法将阻塞 UI 线程,这会导致糟糕的用户体验。所以使用 CancellationTokenSource.CancelAfter方法在您的场景中可能是更好的选择。
以下是我验证过的代码:
public async void downloadFile(string fileUrl, string fileName)
{
var myFolder = await StorageFolder.GetFolderFromPathAsync(Package.Current.InstalledLocation.Path);
var myFile = await myFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
var downloader = new BackgroundDownloader();
var downloadOperation = downloader.CreateDownload(new Uri(fileUrl), myFile);
// Define the cancellation token.
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
cts.CancelAfter(1000);
try
{
// Pass the token to the task that listens for cancellation.
await downloadOperation.StartAsync().AsTask(token);
// file is downloaded in time
}
catch (TaskCanceledException)
{
// timeout is reached, downloadOperation is cancled
}
finally
{
// Releases all resources of cts
cts.Dispose();
}
}
关于c# - 取消 Windows 10 BackgroundDownloader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548454/