我正在用一个异步控制器做一些测试,我有以下代码:
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操作的data参数上的已用时间值。
发布于 2012-08-07 20:12:18
我没有对此进行测试,但是您应该将Decrement调用放入您的匿名任务中。我猜测Decrement是在任务完成之前调用的。
https://stackoverflow.com/questions/11830865
复制相似问题