C# 取消Windows 10后台下载程序

C# 取消Windows 10后台下载程序,c#,win-universal-app,C#,Win Universal App,我想为BackgroundDownloader实现一个超时功能。当我达到超时时,我无法取消下载操作。因此,我这样使用它: public async void downloadFile(string fileUrl, string fileName) { var myFolder = await StorageFolder.GetFolderFromPathAsync(Package.Current.InstalledLocation.Path); var m

我想为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信息:此操作已启动。呼叫 AttachAsync以附加到正在运行的下载/上载

我明白了

引发异常:中的“System.Runtime.InteropServices.COMException” Project.exe WinRT信息:此操作未启动。呼叫 StartAsync以启动操作。其他信息:创建了一个方法 在一个意想不到的时间打电话来

任何想法都将受到赞赏

当我达到超时时,我无法取消下载操作。我正在尝试: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();
    }
}
通常我们用来取消下载操作。此外,使用方法将阻止UI线程,这将导致糟糕的用户体验。所以,在您的场景中,使用方法可能是更好的选择

以下是我已验证的代码:

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