2012-08-06 67 views
0

我做一些测试使用异步控制器和我有以下代码:AsyncController AsyncManager参数问题

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花费1秒多跌少,但我不获取IndexCompleted Action上存储在数据参数上的流逝值。

+0

我不清楚如何执行IndexCompleted - 你从来没有参考过任何地方。 – 2012-08-06 16:10:58

+0

理论上,如果控制器从AsyncController继承,它会尝试查找以Async和Completed结尾以异步执行操作的actionmethod。 我使用了localhost:{port}/AsyncSample/Index,它的执行正常,但IndexComplete操作中的参数数据有一个0值。 – Jose3d 2012-08-07 09:27:42

+0

@ Jose3d你可以简单地看看它是否有效?例如,删除您的长操作,并简单地分配'AsyncManager.Parameters [“data”] = data;' – wal 2012-08-07 11:55:19

回答

2

我没有测试这个,但你应该把Decrement里面你的匿名任务。我的猜测是在任务完成之前调用Decrement