Task.Yield()

作者:追风剑情 发布于:2023-8-11 11:31 分类:C#

Task.Yield()主动把控制权返回给调用方。

using System;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main 1");
            _ = LoadAsync();
            Console.WriteLine("Main 2");

            Console.ReadLine();
        }

        public static async Task LoadAsync()
        {
            Console.WriteLine("LoadAsync 1");
            //主动把控制权返回给调用方
            await Task.Yield();
            Console.WriteLine("LoadAsync 2");
        }
    }
}

运行测试
1111.png


标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号