AsyncController AsyncManager参数问题

时间:2012-08-06 15:02:54

标签: c# asp.net-mvc

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

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上获取存储在data参数上的已用值。

1 个答案:

答案 0 :(得分:2)

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

相关问题