C# Async await和Task的关系

论坛 期权论坛 脚本     
已经匿名di用户   2022-4-13 16:44   2104   0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsyncAwatiConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Example();
            string result = Console.ReadLine();
            Console.WriteLine("You typed: " + result);
        }
        public static async void Example()
        {
            int t = await Task.Run(() => Allocate());
        }

        public static  void Example2()
        {
            Task<int> task = new Task<int>(() =>
            {
                return 11;
            });
            task.Start();
            task.Wait();
        }
        static int Allocate()
        {
            // Compute total count of digits in strings.
            int size = 0;
            for (int z = 0; z < 100; z++)
            {
                for (int i = 0; i < 1000000; i++)
                {
                    string value = i.ToString();
                    if (value == null)
                    {
                        return 0;
                    }
                    size += value.Length;
                }
            }
            return size;
        }
    }
}

  

转载于:https://www.cnblogs.com/alphafly/p/5260184.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP