取消长时间运行的任务延迟

时间:2017-01-27 11:36:20

标签: c# asp.net-web-api async-await task cancellationtokensource

我正在尝试取消由web api请求创建的异步延迟任务(Task.Delay),以及另一个发出取消的web api请求。它看起来不像取消了task.delay的计时器。这是我试图实现的代码片段。为了您的信息,我使用Application对象来存储CancellationTokenSource对象,以跨多个请求检索令牌源。 更新问题是我希望通过从代码中抛出异常来取消任务。但它从未发生过。如何使此代码取消task.delay?

using Microsoft.Practices.Unity;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace WebApplication8.Controllers
{
    public class TaskController : ApiController
    {
        [HttpGet]
        public async Task<string> CreateTask(int id)
        {
            var tokenSource = new CancellationTokenSource();

            var concurrentTokens = GetConcurrentTokens();

            concurrentTokens.TryAdd(id, tokenSource);

            CancellationToken token = tokenSource.Token;
            token.ThrowIfCancellationRequested();

            await Task.Delay(50000,token);

            return "Task Created";
        }


        [HttpGet]
        public async Task<string> Cancel(int id)
        {
            var concurrentTokens = GetConcurrentTokens();
            CancellationTokenSource item = concurrentTokens.First(t => t.Key == id).Value;
            item.Cancel();
            item.Dispose();
            var tokenSource2 = new CancellationTokenSource();
            concurrentTokens.TryRemove(id,out tokenSource2);
            return "Cancelled";
        }

        private ConcurrentDictionary<long, CancellationTokenSource> GetConcurrentTokens()
        {
            var tokens = HttpContext.Current.Application["Tokens"];

             if (tokens == null)
             {
                tokens = new ConcurrentDictionary<long, CancellationTokenSource>();
                HttpContext.Current.Application["Tokens"] = tokens;
             }

             return (ConcurrentDictionary<long, CancellationTokenSource>) tokens;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

我认为它被取消了,您可以通过添加try catch来尝试:

 try
  {
     await Task.Delay(5000, token);
   }
  catch(TaskCanceledException ex)
    {

    }

你会看到它进入catch块,该方法不返回任何东西,因为TaskCanceledException

答案 1 :(得分:0)

您的代码看起来是正确的,我测试了这样:

var tc = new TaskController();
var backTask1 = tc.CreateTask(1);
var backTask2 = tc.CreateTask(2);   
// task 2 gets cancelled         
await tc.Cancel(2);
try
{
    var res2 = await backTask2;
}
catch (OperationCanceledException) { }
// task 1 waits
await backTask1;

n.b。的是:

  • token.ThrowIfCancellationRequested()在创建CancellationTokenSource之后什么都不做 - 如果某些代码已经取消了源代码,这个方法就会抛出异常。
  • 如果任务是在没有等待的情况下启动的,那么在等待它之前,您不会看到它引发的异常。

答案 2 :(得分:0)

尝试使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="panel">Hello world!</div> <div id="flip"> <span class="f">MENU</span> </div>。它适用于我,但我不确定这是否是你想要的。

TaskCompletionSource

此外,无需保留var source = new CancellationTokenSource(); var concurrentTokens = GetConcurrentTokens(); concurrentTokens.TryAdd(id, source); var completionSource = new TaskCompletionSource<object>(); source.Token.Register(() => completionSource.TrySetCanceled()); var task = Task.Delay(50000, source.Token); // Continue when any of these are done. await Task.WhenAny(task, completionSource.Task); if (task.IsCanceled) { return "Task was not created"; } return "Task Created"; Cancel。您可以返回字符串而不是async