代码改变世界

关于 Task.Run 简单的示例

2018-09-09 17:27  音乐让我说  阅读(3081)  评论(0编辑  收藏  举报

1. 关于 Task.Run 简单的示例01

直接贴代码了:

     public static class TaskDemo01
    {
        public static void Run()
        {
            Console.WriteLine("【Debug】主 线程ID:" + Thread.CurrentThread.ManagedThreadId);
            string str1 = string.Empty, str2 = string.Empty, str3 = string.Empty;

            var task1 = Task.Run(() =>
            {
                Thread.Sleep(2000);
                str1 = "姓名:张三,";
                Console.WriteLine("【Debug】task1 线程ID:" + Thread.CurrentThread.ManagedThreadId);
            }).ContinueWith(t =>
            {
                Thread.Sleep(2000);
                str2 = "年龄:25,";
                Console.WriteLine("【Debug】task2 线程ID:" + Thread.CurrentThread.ManagedThreadId);
            }).ContinueWith(t =>
            {
                Thread.Sleep(2000);
                str3 = "爱好:妹子";
                Console.WriteLine("【Debug】task3 线程ID:" + Thread.CurrentThread.ManagedThreadId);
            });

            //Thread.Sleep(2500);//其他逻辑代码

            task1.Wait();

            Console.WriteLine(str1 + str2 + str3);
            Console.WriteLine("【Debug】主 线程ID:" + Thread.CurrentThread.ManagedThreadId);
        }
    }

 

运行截图:

2. 关于 Task.Run 简单的示例02

直接贴代码了:

    public static class TaskDemo02
    {
        /// <summary>
        /// 整个方法只需用 5 秒即可执行完毕!
        /// </summary>
        public static void Run()
        {
            Console.WriteLine(string.Format("当前时间:{0},主线程方法开始...!", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            var task = Task.Run(() =>
            {
                Thread.Sleep(5000);//假设这是个访问数据库的方法
                Console.WriteLine(string.Format("当前时间:{0},Task方法体内部执行完毕!", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            });
            Console.WriteLine(string.Format("当前时间:{0},主线程方法已经注入 Task.Run 方法,即将休眠 2 秒。", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            Thread.Sleep(2000);//假设这是个访问FQ网站的方法
            Console.WriteLine(string.Format("当前时间:{0},主线程方法休眠 2 秒完成。即将等待 Task.Run 方法执行结束", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            task.Wait();
            Console.WriteLine(string.Format("当前时间:{0},主线程方法整个方法执行完毕!!", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
        }

    }

 

运行截图:

3. 关于 Task.Run 简单的示例03

        public static void Run()
        {
            var url = "http://www.cnblogs.com?id=";

            var list_task = new List<Task<string>>();
            for (int i = 0; i < 10; i++)
            {
                var temp_url = url + i;
                var task = Task.Run(async () =>
                {
                    Console.WriteLine("temp_url=" + temp_url);
                    var client = new System.Net.Http.HttpClient();
                    var html = await client.GetStringAsync(temp_url);
                    Console.WriteLine("temp_url=" + temp_url + " is ok");
                    return html;
                });

                list_task.Add(task);
            }

            Task.WaitAll(list_task.ToArray());

            foreach (var item in list_task)
            {
                Console.WriteLine(item.Result);
            }
        }

 

4.

5.

直接贴代码了:

运行截图:

谢谢浏览!