0

我正在使用异步控制器进行一些测试,并且我有以下代码:

public class AsyncSampleController : AsyncController
{

     public void IndexAsync()
      {
           Tasks tasks = new Tasks();
           //Indicates that we already started an asynchronous operation
           AsyncManager.OutstandingOperations.Increment();
           //Task.Factory start new to use another thread to use our operation.
           Task.Factory.StartNew(() =>
           {
                Stopwatch s1 = Stopwatch.StartNew();        
                tasks.BigOperation();
                s1.Stop();
                long data=s1.ElapsedMilliseconds;
                AsyncManager.Parameters.Add("data",data);
            });
            AsyncManager.OutstandingOperations.Decrement();

       }
       public ActionResult IndexCompleted(long data)
       { 
           ViewBag.ElapsedTime = data.ToString();
           return View();
       }
}

问题是 BigOperation 方法或多或少花费了一秒钟,但我没有得到存储在 IndexCompleted Action 的数据参数上的经过值。

4

1 回答 1

2

我没有对此进行测试,但您应该将Decrement呼叫放入您的匿名任务中。我的猜测是Decrement在任务完成之前调用。

于 2012-08-07T12:12:17.730 回答