c# - 取消 Windows 10 BackgroundDownloader

标签 c# win-universal-app

我想为 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/

相关文章:

c# - 如何在.NET中进行交互式短信?

c# - 滚动时拖放事件会中断 GUI 的平滑运动

c# - 获取类类型?

c# - 'Windows.Storage.StorageFile File = await FilePicker.PickSingleFileAsync()' 不工作

c# - 从本地xampp mysql-database获取数据

c# - 启动时运行后台任务

c# - 我如何声明派生的 "shell"类,它们除了重命名外什么都不做?

c# - 如何在 UWP MVVM 中使用 CalenderView

c# - 依赖属性更改时 StackPanel 可见性未更新

windows-phone-8.1 - list 中具有共享用户证书功能的 Windows Phone 8.1 应用程序中的部署错误