我正在使用异步控制器进行一些测试,并且我有以下代码:
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 的数据参数上的经过值。