csharp Asynch&Await(简单).cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Asynch&Await(简单).cs相关的知识,希望对你有一定的参考价值。

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

namespace Async_and_Await___Simple
{
    class Program
    {
        static void Main(string[] args)
        {
            //async and await are nothing but Code Markers which mark code positions from where control should resume after a taks(thread) completes.
            //What are Async and Await ( .NET 4.5 Interview question with answers)?
            //https://www.youtube.com/watch?v=V2sMXJnDEjM

            Console.WriteLine("BEFORE");
            Method();
            Console.WriteLine("AFTER");
            Console.ReadKey();
        }

        private static async void Method() //method must be async (which contains an await marker at least)
        {
            //put a debug  point here > start debugging > press f11 again and again 
            await Task.Run(new Action(LongTask));
            Console.WriteLine("New Thread");
        }

        public static void LongTask()
        {
            Thread.Sleep(15000);
        }
    }
}

以上是关于csharp Asynch&Await(简单).cs的主要内容,如果未能解决你的问题,请参考以下文章

kotlin的suspend对比csharp的async&await

csharp async / awaitの例

csharp 异步/ AWAIT

温故知新,CSharp遇见异步编程(Async/Await),聊聊异步编程最佳做法

Python使用async/await极简例子

csharp C#5.0 async / awaitでの主要メソッドの书き方