2015年10月22日 星期四

ConfigureAwait.aspx

ConfigureAwait.aspx
<%@ Page Language="C#" AutoEventWireup="true" Async="true" CodeBehind="ConfigureAwait.aspx.cs" Inherits="ConcurrencyWebForm.ConfigureAwait" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnSyncWithoutConfigureAwait" runat="server" Text="SyncWithoutConfigureAwait" OnClick="btnSyncWithoutConfigureAwait_Click" />
            <br />
            <asp:Button ID="btnSyncWithConfigureAwait" runat="server" Text="SyncWithConfigureAwait" OnClick="btnSyncWithConfigureAwait_Click" />
            <br />
            <asp:Button ID="btnAsyncWithConfigureAwait" runat="server" Text="AsyncWithConfigureAwait" OnClick="btnAsyncWithConfigureAwait_Click" />
        </div>
    </form>
</body>
</html>

ConfigureAwait.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ConcurrencyWebForm
{
    public partial class ConfigureAwait : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        
        //此例會造成Deadlock        
        //ps.以同步的方式執行非同步的function是非常不建議的
        protected void btnSyncWithoutConfigureAwait_Click(object sender, EventArgs e)
        {            
            Task tVal = DoSomethingAsync();
            string abc = "";
            string cde = "";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + tVal.Result.ToString() + "');", true);
        }

        private async Task DoSomethingAsync()
        {
            int val = 13;

            // Asynchronously wait 3 second.
            //只是為了讓此library function為非同步執行才用Task.Delay,實際尚無特別意義
            await Task.Delay(TimeSpan.FromSeconds(3));

            val *= 2;

            // Asynchronously wait 3 second.
            await Task.Delay(TimeSpan.FromSeconds(3));
            
            return val;
        }



        protected void btnSyncWithConfigureAwait_Click(object sender, EventArgs e)
        {
            Task tVal = DoSomethingAsyncWithConfigureAwait();
            string abc = "";
            string cde = "";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + tVal.Result.ToString() + "');", true);
        }

        //.ConfigureAwait(false)另開了context以儲存library function的執行狀態        
        private async Task DoSomethingAsyncWithConfigureAwait()
        {
            int val = 13;

            // Asynchronously wait 3 second.     
            //增加了.ConfigureAwait(false)另開了context
            await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(false);

            val *= 2;

            // Asynchronously wait 3 second.
            //增加了.ConfigureAwait(false)另開了context
            await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(false);

            return val;
        }

        protected async void btnAsyncWithConfigureAwait_Click(object sender, EventArgs e)
        {
            int intVal = await DoSomethingAsyncWithConfigureAwait();
            string abc = "";
            string cde = "";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + intVal.ToString() + "');", true);
        }

      
    }
}

沒有留言:

張貼留言