Debugging notes for async + await + task asynchronous mechanism of C ා 5.0 (. After net 4.5)

Time:2020-10-30

1.) no return value (asynchronous is also thread based).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConfigLab.Test.BLL.AsyncTaskSpace
{
    /// 
    ///Function Description: test asynchronous processing mechanism of async + wait + task with no return value and no waiting.
    ///Creation time: August 21, 2020
    ///Created by: PCW
    ///Blog: http://cnblogs.com/taohuadaozhu
    /// 
    public  class Test1_Async_NotWait
    {
        public static void StartTest()
        {
            Utils.SaveLog ($"Test1_ Async_ NotWait.StartTest . start{ DateTime.Now.ToString ("yyyy MM DD HH: mm: SS FFF")}, thread:{ Thread.CurrentThread.ManagedThreadId }");
            AsyncTestMethod();
            Utils.SaveLog ($"Test1_ Async_ NotWait.StartTest End{ DateTime.Now.ToString ("yyyy MM DD HH: mm: SS FFF")}, thread:{ Thread.CurrentThread.ManagedThreadId }");
        }
        /// 
        ///Asynchronous method
        /// 
        /// 
        static async Task AsyncTestMethod()
        {
            /*
             Note 1: when using the await expression, the method returns before it gets here. After the control returns to the thread calling the method, the control will automatically return to the statement under the await associated statement. When an exception occurs, the exception is thrown in the await expression
             Note 2: use Task.Run Mode, the asynchronous task is executed in a separate thread.
             */
            Utils.SaveLog ($"Test1_ Async_ NotWait.AsyncTestMethod.begin : await  Task.Run , thread:{ Thread.CurrentThread.ManagedThreadId }");
            await Task.Run(() => {
                for (int i = 0; i < 5; i++)
                {
                    Thread.Sleep(200);
                    Utils.SaveLog ($"Test1_ Async_ NotWait.AsyncTestMethod :{i},{ DateTime.Now.ToString ("yyyy MM DD HH: mm: SS FFF")}, thread:{ Thread.CurrentThread.ManagedThreadId }");
                }
            });
            Utils.SaveLog ($"Test1_ Async_ NotWait.AsyncTestMethod.after : await  Task.Run , thread:{ Thread.CurrentThread.ManagedThreadId }");
        }
    }
}

 

[2020-08-21 00:41:33]Test1_ Async_ NotWait.StartTest . start.. 2020-08-21 00:41:33 742, thread: 1
[2020-08-21 00:41:33]Test1_ Async_ NotWait.AsyncTestMethod.begin : await Task.Run , thread: 1
[2020-08-21 00:41:33]Test1_ Async_ NotWait.StartTest . end.. 2020-08-21 00:41:33 815, thread: 1
[2020-08-21 00:41:34]Test1_ Async_ NotWait.AsyncTestMethod : 02020-08-21 00:41:34 017, thread: 3
[2020-08-21 00:41:34]Test1_ Async_ NotWait.AsyncTestMethod : 12020-08-21 00:41:34 219, thread: 3
[2020-08-21 00:41:34]Test1_ Async_ NotWait.AsyncTestMethod : 22020-08-21 00:41:34 422, thread: 3
[2020-08-21 00:41:34]Test1_ Async_ NotWait.AsyncTestMethod : 32020-08-21 00:41:34 624, thread: 3
[2020-08-21 00:41:34]Test1_ Async_ NotWait.AsyncTestMethod : 42020-08-21 00:41:34 827, thread: 3
[2020-08-21 00:41:34]Test1_ Async_ NotWait.AsyncTestMethod.after : await Task.Run , thread: 1

 

2.) when there is a return value (asynchronous is also thread based).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConfigLab.Test.BLL.AsyncTaskSpace
{
    /// 
    ///Function Description: test asynchronous processing mechanism of async + wait + task with return value and no need to wait.
    ///Creation time: August 21, 2020
    ///Created by: PCW
    ///Blog: http://cnblogs.com/taohuadaozhu
    /// 
    public class Test3_Async_TaskWithReturn
    {
        public static  void StartTest()
        {
            Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.StartTest . start{ DateTime.Now.ToString ("yyyy MM DD HH: mm: SS FFF")}, thread:{ Thread.CurrentThread.ManagedThreadId }");
            getTestMethodReturnValue();
            Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.StartTest End{ DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss fff")}");
        }
        static async void getTestMethodReturnValue()
        {
            Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.getTestMethodReturnValue . start{ DateTime.Now.ToString ("yyyy MM DD HH: mm: SS FFF")}, thread:{ Thread.CurrentThread.ManagedThreadId }");
            var taskResult= AsyncTestMethod();
            await taskResult;
            // Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.StartTest End{ DateTime.Now.ToString (yyyy MM DD HH: mm: SS FFF ")}, the asynchronous task returns the following result:{( task.Status ! TaskStatus.WaitingForActivation?  task.Result :"")}");
            Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.getTestMethodReturnValue End{ DateTime.Now.ToString (yyyy MM DD HH: mm: SS FFF ")}, the asynchronous task returns the following result:{ taskResult.Result }, thread:{ Thread.CurrentThread.ManagedThreadId }");
        }

        /// 
        ///Asynchronous method
        /// 
        /// 
        static async Task AsyncTestMethod()
        {
            /*
             Note 1: when await expression is used, the control will return to the thread calling this method; after the waiting method is executed, the control will automatically return to the following statement. When an exception occurs, the exception is thrown in the await expression
             Note 2: use Task.Run Mode, the asynchronous task is executed in a separate thread.
             */
            Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.AsyncTestMethod ..(await  Task.Run Before), thread:{ Thread.CurrentThread.ManagedThreadId }");
            return await Task.Run(() => {
                Thread.Sleep(2000);
                Utils.SaveLog ($"Test3_ Async_ TaskWithReturn.AsyncTestMethod ( task.Run In the middle){ DateTime.Now.ToString ("yyyy MM DD HH: mm: SS FFF")}, thread:{ Thread.CurrentThread.ManagedThreadId }");
                return "success";
             });
        }
    }
}

[2020-08-21 00:41:35]Test3_ Async_ TaskWithReturn.StartTest . start.. 2020-08-21 00:41:35 821, thread: 1
[2020-08-21 00:41:35]Test3_ Async_ TaskWithReturn.getTestMethodReturnValue . start.. 2020-08-21 00:41:35 824, thread: 1
[2020-08-21 00:41:35]Test3_ Async_ TaskWithReturn.AsyncTestMethod ..(await Task.Run Before), thread: 1
[2020-08-21 00:41:35]Test3_ Async_ TaskWithReturn.StartTest . end.. 2020-08-21 00:41:35 829
[2020-08-21 00:41:37]Test3_ Async_ TaskWithReturn.AsyncTestMethod ( task.Run Among them), 2020-08-21 00:41:37 830, thread: 3
[2020-08-21 00:41:37]Test3_ Async_ TaskWithReturn.getTestMethodReturnValue . end.. 2020-08-21 00:41:37 834, asynchronous task return result: success, thread: 1