C# Async await和Task的关系

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;
        }
    }
}

  

posted @ 2016-03-09 23:07  ICupid  阅读(3276)  评论(0编辑  收藏  举报