MVC 5中Async和await使用

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>

运行结果如图:

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


这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值