一段时间后如何取消异步任务

Igo*_*man 2 c# async-await microsoft-metro windows-runtime

在我的 Windows Store 应用程序中,我有一个方法

public async static Task InitAds()
{
    Debug.WriteLine("API: Loading Ad images");
    await Task.WhenAll(ads.Select(l => l.Value).Where(l=>l!=null).Select(l => l.StartRotation()));
 }
Run Code Online (Sandbox Code Playgroud)

我用来下载和初始化(下载,解析|项目中的广告。调用时等待此方法

...
await AdReader.InitAds()
...
Run Code Online (Sandbox Code Playgroud)

问题是广告服务器有时响应非常缓慢。我想要一个超时时间,比如说这个方法运行 10 秒。如果它没有在这个超时时间内完成,我希望它被杀死并且我的代码继续。

实现这一点的最佳方法是什么?我找到了如何在等待中取消任务?但它使用 TaskFactory 并且当我尝试这种方法并在 Task.Run 中调用我的方法时,它不会等待并且代码继续。

编辑:

StartRotation 也是一个异步方法调用另一个异步方法

public async Task StartRotation(CancellationToken ct)
{
        if (Images.Count == 1)
        {
            await Image.LoadAndSaveImage(ct);
        }

        if (Images.Count <2) return;

        foreach (var img in Images)
        {
            await img.LoadAndSaveImage(ct);
        }

        Delay = Image.Delay;
        DispatcherTimer dt = new DispatcherTimer();
        dt.Interval = TimeSpan.FromMilliseconds(Delay);
        dt.Tick += (s, e) =>
        {
            ++index;
            if (index > Images.Count - 1)
            {
                index = 0;
            }
            Image = Images[index];
        };
        dt.Start();
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*ary 5

取消是合作的。你只需要传入CancellationToken你的StartRotation

public async static Task InitAds(CancellationToken token)
{
  Debug.WriteLine("API: Loading Ad images");
  await Task.WhenAll(ads.Select(l => l.Value).Where(l=>l!=null).Select(l => l.StartRotation(token)));
}
Run Code Online (Sandbox Code Playgroud)

然后这样称呼它:

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await InitAds(cts.Token);
Run Code Online (Sandbox Code Playgroud)

  • 是的。如果你有一个不带 `CancellationToken` 的 `async` 方法,那么这个方法就不能被取消。您可以编写一个可以“取消”的包装器(并使其提前返回),但不可取消的 `async` 方法将继续运行。 (3认同)