MVC 5中Async和await使用
来源:互联网 发布:泰州网络公关技巧 编辑:程序博客网 时间:2024/06/05 09:52
HomeController.cs
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Threading;using System.Threading.Tasks;using System.Web;using System.Web.Mvc;namespace Async.Controllers{ public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult GetList() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountry(); var state = GetState(); var city = GetCity(); watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } public async Task<ActionResult> GetListAsync() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountryAsync(); var state = GetStateAsync(); var city = GetCityAsync(); var content = await country; var count = await state; var name = await city; watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } #region GetCountry方法GetList && GetListAsync public string GetCountry() { Thread.Sleep(3000); //使用 - 当你想阻塞当前线程 return "India"; } public async Task<string> GetCountryAsync() { await Task.Delay(3000); //使用 - 当您想要逻辑延迟而不阻塞当前线程 return "India"; } #endregion #region GetState方法用于GetList && GetListAsync public string GetState() { Thread.Sleep(5000); //使用 - 当你想阻塞当前线程 return "Gujarat"; } public async Task<string> GetStateAsync() { await Task.Delay(5000); //使用 - 当您想要逻辑延迟而不阻塞当前线程 return "Gujarat"; } #endregion #region GetCity方法GetList && GetListAsync public string GetCity() { Thread.Sleep(6000); //使用 - 当你想阻塞当前线程 return "Junagadh"; } public async Task<string> GetCityAsync() { await Task.Delay(6000); //使用 - 当您想要逻辑延迟而不阻塞当前线程 return "Junagadh"; } #endregion }}
Index.cshtml
@{ ViewBag.Title = "Index";}<link href="~/Content/docs.css" rel="stylesheet" /><link href="~/Content/codemirror.css" rel="stylesheet" /><script src="~/Content/codemirror.js"></script><script src="~/Content/edit/matchbrackets.js"></script><link href="~/Content/hint/show-hint.css" rel="stylesheet" /><script src="http://localhost:53054/Content/hint/show-hint.js"></script><script src="~/Content/clike.js"></script><style> .CodeMirror { border: 2px inset #dee; }</style><br /> <br /><div class="row"> @using (Html.BeginForm("GetList", "Home", FormMethod.Get)) { <div class="col-md-6 col-sm-12"> <h2>Synchronize Method</h2> <div> <textarea id="c-code"> public ActionResult GetList() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountry(); var state = GetState(); var city = GetCity(); watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } </textarea> </div> <br /> <button type="submit" class="btn btn-primary">Get Excecution Time</button> @if (ViewBag.WatchMilliseconds != null) { <h1 class="label label-danger">@ViewBag.WatchMilliseconds 毫秒</h1> } </div> } @using (Html.BeginForm("GetListAsync", "Home", FormMethod.Get)) { <div class="col-md-6 col-sm-12"> <h2>Asynchronize Method</h2> <div> <textarea id="c-code2"> public async Task<actionresult> GetListAsync() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountryAsync(); var state = GetStateAsync(); var city = GetCityAsync(); var content = await country; var count = await state; var name = await city; watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } </textarea> </div> <br /> <button type="submit" class="btn btn-primary">Get Excecution Time</button> @if (ViewBag.WatchMilliseconds != null) { <h1 class="label label-danger">@ViewBag.WatchMilliseconds 毫秒</h1> } </div> } <script> var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), { lineNumbers: true, matchBrackets: true, mode: "text/x-csrc" }); var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code2"), { lineNumbers: true, matchBrackets: true, mode: "text/x-csrc" }); </script></div>
GetList.cshtml
@{ ViewBag.Title = "Index";}<link href="~/Content/docs.css" rel="stylesheet" /><link href="~/Content/codemirror.css" rel="stylesheet" /><script src="~/Content/codemirror.js"></script><script src="~/Content/edit/matchbrackets.js"></script><link href="~/Content/hint/show-hint.css" rel="stylesheet" /><script src="http://localhost:53054/Content/hint/show-hint.js"></script><script src="~/Content/clike.js"></script><style> .CodeMirror { border: 2px inset #dee; }</style><br /> <br /><div class="row"> @using (Html.BeginForm("GetList", "Home", FormMethod.Get)) { <div class="col-md-6 col-sm-12"> <h2>同步方法</h2> <div> <textarea id="c-code"> public ActionResult GetList() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountry(); var state = GetState(); var city = GetCity(); watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } </textarea> </div> <br /> <button type="submit" class="btn btn-primary">Get Excecution Time</button> @if (ViewBag.WatchMilliseconds != null) { <h1 class="label label-danger">@ViewBag.WatchMilliseconds 毫秒</h1> } </div> } @using (Html.BeginForm("GetListAsync", "Home", FormMethod.Get)) { <div class="col-md-6 col-sm-12"> <h2>异步方法</h2> <div> <textarea id="c-code2"> public async Task<actionresult> GetListAsync() { //Create a stopwatch for getting excution time var watch = new Stopwatch(); watch.Start(); var country = GetCountryAsync(); var state = GetStateAsync(); var city = GetCityAsync(); var content = await country; var count = await state; var name = await city; watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } </textarea> </div> <br /> <button type="submit" class="btn btn-primary">获取执行时间</button> </div> } <script> var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), { lineNumbers: true, matchBrackets: true, mode: "text/x-csrc" }); var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code2"), { lineNumbers: true, matchBrackets: true, mode: "text/x-csrc" }); </script></div>
GetListAsync.cshtml
@{ ViewBag.Title = "Index";}<link href="~/Content/docs.css" rel="stylesheet" /><link href="~/Content/codemirror.css" rel="stylesheet" /><script src="~/Content/codemirror.js"></script><script src="~/Content/edit/matchbrackets.js"></script><link href="~/Content/hint/show-hint.css" rel="stylesheet" /><script src="http://localhost:53054/Content/hint/show-hint.js"></script><script src="~/Content/clike.js"></script><style> .CodeMirror { border: 2px inset #dee; }</style><br /> <br /><div class="row"> @using (Html.BeginForm("GetList", "Home", FormMethod.Get)) { <div class="col-md-6 col-sm-12"> <h2>同步方法</h2> <div> <textarea id="c-code"> public ActionResult GetList() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountry(); var state = GetState(); var city = GetCity(); watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } </textarea> </div> <br /> <button type="submit" class="btn btn-primary">获取执行时间</button> </div> } @using (Html.BeginForm("GetListAsync", "Home", FormMethod.Get)) { <div class="col-md-6 col-sm-12"> <h2>异步方法</h2> <div> <textarea id="c-code2"> public async Task<actionresult> GetListAsync() { //创建一个秒表来获取执行时间 var watch = new Stopwatch(); watch.Start(); var country = GetCountryAsync(); var state = GetStateAsync(); var city = GetCityAsync(); var content = await country; var count = await state; var name = await city; watch.Stop(); ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds; return View(); } </textarea> </div> <br /> <button type="submit" class="btn btn-primary">获取执行时间</button> @if (ViewBag.WatchMilliseconds != null) { <h1 class="label label-danger">@ViewBag.WatchMilliseconds 毫秒</h1> } </div> } <script> var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code"), { lineNumbers: true, matchBrackets: true, mode: "text/x-csrc" }); var cEditor = CodeMirror.fromTextArea(document.getElementById("c-code2"), { lineNumbers: true, matchBrackets: true, mode: "text/x-csrc" }); </script></div>
运行结果如图:
同步方法执行时间累加,异步方法执行时间取任何方法的最长等待时间
阅读全文
0 0
- MVC 5中Async和await使用
- MVC(七、action 中使用await和async)
- .NET中使用异步Async和Await的代价
- .net4.5使用async和await异步编程实例
- .net4.5使用async和await异步编程实例
- .NET中async与await的使用
- 在node.js 中使用async await
- 初探es7中async和await方法
- 使用 Async 和 Await 的异步编程
- 快速理解和使用 ES7 await/async
- 快速理解和使用 ES7 await/async
- async/await使用心得
- Async 和 Await 技术
- await 和 Async 初探
- async和await
- Async和Await
- async 和 await
- async和await 用法
- linux redis-4.0.1 图解安装教程
- checkbox后台增删
- 一个简洁的通用前端输入数据校验方法
- Hibernate与 MyBatis的比较
- 创建DataGuard为什么要开启force logging
- MVC 5中Async和await使用
- Python之Scrapy框架Redis实现分布式爬虫详解
- Sublime3 Text 如何安装插件以及常用的插件必备
- 架构师之路-创业互联网公司如何搭建自己的技术架构
- Unity3D 使用ShareSDK进行QQ授权登录
- ubuntu screen 使用方法
- C# 中的委托和事件
- 易飞9.0.8官方正版安装程序下载地址
- 第七章:idea配置git