Await Async Task

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=======Start Main!==============");
            AsyncMethod();
            Console.WriteLine("========End Main!===============");
            Console.ReadKey();
        }


        // Async action
        private static async void AsyncMethod()
        {
            Console.WriteLine("==========Start Async!============");
            var result = await GetResultAsync();

            Console.WriteLine("Result:{0}", result);
            Console.WriteLine("=========End Async!=========");
        }

        public static async Task<int> GetResultAsync()
        {
            var result = await Task.Run(() => Sum(10000));
            return result;
        }

        public static Int32 Sum(Int32 n)
        {
            Int32 sum = 0;
            for (; n > 0; n--)
            {
                checked
                {
                    sum += n;
                }
            }
            return sum;
        }

 

posted @ 2013-08-25 23:03  chunchill  阅读(545)  评论(0编辑  收藏  举报