加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC4异步和并行执行

发布时间:2020-12-15 22:58:07 所属栏目:asp.Net 来源:网络整理
导读:所以我试图让我的头脑围绕这个新的“异步”东西在.net 4.5.我以前玩异步控制器和任务并行库,并清理了这段代码: 采取这种模式: public class TestOutput{ public string One { get; set; } public string Two { get; set; } public string Three { get; set
所以我试图让我的头脑围绕这个新的“异步”东西在.net 4.5.我以前玩异步控制器和任务并行库,并清理了这段代码:

采取这种模式:

public class TestOutput
{
    public string One { get; set; }
    public string Two { get; set; }
    public string Three { get; set; }

    public static string DoWork(string input)
    {
        Thread.Sleep(2000);
        return input;
    }
}

在这样的控制器中使用哪一个:

public void IndexAsync()
{
    AsyncManager.OutstandingOperations.Increment(3);

    Task.Factory.StartNew(() => 
        { 
            return TestOutput.DoWork("1"); 
        })
        .ContinueWith(t => 
        { 
            AsyncManager.OutstandingOperations.Decrement(); 
            AsyncManager.Parameters["one"] = t.Result; 
        });
    Task.Factory.StartNew(() =>
        {
            return TestOutput.DoWork("2");
        })
        .ContinueWith(t =>
        {
            AsyncManager.OutstandingOperations.Decrement();
            AsyncManager.Parameters["two"] = t.Result;
        });
    Task.Factory.StartNew(() =>
        {
            return TestOutput.DoWork("3");
        })
        .ContinueWith(t =>
        {
            AsyncManager.OutstandingOperations.Decrement();
            AsyncManager.Parameters["three"] = t.Result;
        });
}

public ActionResult IndexCompleted(string one,string two,string three)
{
    return View(new TestOutput { One = one,Two = two,Three = three });
}

由于TPL的魔力,该控制器会在2秒内呈现视图.

现在,我预计(相当天真地),上面的代码将转换为以下内容,使用C#5的新“异步”和“等待”功能:

public async Task<ActionResult> Index()
{
    return View(new TestOutput
    {
        One = await Task.Run(() =>TestOutput.DoWork("one")),Two = await Task.Run(() =>TestOutput.DoWork("two")),Three = await Task.Run(() =>TestOutput.DoWork("three"))
    });
}

该控制器在6秒内呈现视图.在翻译的某个地方,代码变得不再平行.我知道异步和并行是两个不同的概念,但不知何故我认为代码将工作相同.有人可以指出这里发生了什么,如何解决?

解决方法

Somewhere in the translation the code became no longer parallel.

恰恰.等待(异步)等待单个操作完成.

并行异步操作可以通过启动实际的任务来完成,但不等待它们直到以后:

public async Task<ActionResult> Index() 
{
  // Start all three operations.
  var tasks = new[]
  {
    Task.Run(() =>TestOutput.DoWork("one")),Task.Run(() =>TestOutput.DoWork("two")),Task.Run(() =>TestOutput.DoWork("three"))
  };

  // Asynchronously wait for them all to complete.
  var results = await Task.WhenAll(tasks);

  // Retrieve the results.
  return View(new TestOutput
  {
    One = results[0],Two = results[1],Three = results[2]
  }); 
}

附:还有一个Task.WhenAny.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读