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>

运行结果如图:

同步方法执行时间累加,异步方法执行时间取任何方法的最长等待时间
这里写图片描述


这里写图片描述