小编jdk*_*zek的帖子

检测ASP.NET MVC中的中止请求

在ASP.NET MVC中的异步控制器中,是否有任何方法可以判断客户端是否/何时中止请求?

[NoAsyncTimeout]
public void IndexAsync() {
  var source = new CancellationTokenSource();

  Task.Factory.StartNew(() => {
    while (true) {
      if (source.Token.IsCancellationRequested) {
        AsyncManager.Finish();
        return;
      }

      Response.Write(".");
      Thread.Sleep(1000);
    }
  }, source.Token);

  // Is there any way to do this?
  Request.Aborted += (sender, e) => source.Cancel();

  AsyncManager.OutstandingOperations.Increment();
}
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asynchronous

8
推荐指数
1
解决办法
3815
查看次数

标签 统计

asp.net-mvc ×1

asynchronous ×1