.net多线程 Task

1.基本用法

1  //Task.Run(() => this.DoSomethingLong("btnTask_Click1"));
2             //Task.Run(() => this.DoSomethingLong("btnTask_Click2"));
3             //TaskFactory taskFactory = Task.Factory;//4.0
4             //taskFactory.StartNew(() => this.DoSomethingLong("btnTask_Click3"));
5             //new Task(() => this.DoSomethingLong("btnTask_Click4")).Start();

2.线程等待,task.WaitAll

 1 ////什么时候用多线程? 任务能并发运行;提升速度;优化体验
 2                 List<Task> taskList = new List<Task>();
 3                 Console.WriteLine($"项目经理启动一个项目。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
 4                 Console.WriteLine($"前置的准备工作。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
 5                 Console.WriteLine($"开始编程。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
 6                 taskList.Add(Task.Run(() => this.Coding("爱书客", "Client")));//task--hash--
 7                 taskList.Add(Task.Run(() => this.Coding("风动寂野", "Portal")));
 8                 taskList.Add(Task.Run(() => this.Coding("笑看风云", "Service")));
 9                 //做个子类 子类里面包含了一个属性  绿叶种子写个例子
10                 taskList.Add(Task.Run(() => this.Coding("Jack", "Jump")));
11                 taskList.Add(Task.Run(() => this.Coding("胡萝卜", "Monitor")));
12                 Task.WaitAll(taskList.ToArray());

 3.等限定的时间,然后继续 

1   ////Task.WaitAll(taskList.ToArray(), 1000);//限时等待

4.等待某一个

1    //阻塞:需要完成后再继续
2                 //Task.WaitAny(taskList.ToArray());//会阻塞当前线程,等着某个任务完成后,才进入下一行  卡界面
3                 ////Task.WaitAny(taskList.ToArray(), 1000);
4                 //Console.WriteLine($"完成里程碑 【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");

5.不卡主线程等待 WhenAll WhenAny

 1  //taskList.Add(Task.WhenAny(taskList.ToArray()).ContinueWith(t =>
 2                 //{
 3                 //    Console.WriteLine(taskList.ToArray().FirstOrDefault(s => s.Status == TaskStatus.RanToCompletion));
 4                 //    Console.WriteLine($"得意的笑。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
 5                 //}));
 6 
 7                 //taskList.Add(Task.WhenAll(taskList.ToArray()).ContinueWith(t =>
 8                 //{
 9                 //    Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
10                 //}));

6.上述taskFactory 写法

 1                 ////TaskFactory taskFactory = new TaskFactory();
 2                 ////taskFactory.ContinueWhenAll(taskList.ToArray(), tList =>
 3                 ////{
 4                 ////    Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
 5                 ////});
 6 
 7                 ////taskFactory.ContinueWhenAny(taskList.ToArray(), t =>
 8                 ////{
 9                 ////    Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
10                 ////});

7.查看是哪个线程先完成任务的,taskFactory.StartNew的重载方法,可以拿到自定义的标识

 1  //TaskFactory taskFactory = new TaskFactory();
 2                 //List<Task> taskList = new List<Task>();
 3                 //taskList.Add(taskFactory.StartNew(o => this.Coding("爱书客", "Client"), "爱书客"));
 4                 //taskList.Add(taskFactory.StartNew(o => this.Coding("风动寂野", "Portal"), "风动寂野"));
 5                 //taskList.Add(taskFactory.StartNew(o => this.Coding("笑看风云", "Service"), "笑看风云"));
 6                 //taskFactory.ContinueWhenAny(taskList.ToArray(), t =>
 7                 //{
 8                 //Console.WriteLine(t.AsyncState);
 9                 //Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
10                 //});
11                 //taskFactory.ContinueWhenAll(taskList.ToArray(), tList =>
12                 //{
13                 //Console.WriteLine(tList[0].AsyncState);
14                 //Console.WriteLine($"部署环境,联调测试。。。【{Thread.CurrentThread.ManagedThreadId.ToString("00")}】");
15                 //});

 8.回调 

1 Task.Run(() => this.Coding("爱书客", "Client")).ContinueWith(t => { });

9.延迟和等待,卡界面 和不卡界面

 1  //{
 2             //    //Task.Delay(1000);//延迟  不会卡
 3             //    //Thread.Sleep(1000);//等待   卡
 4 
 5             //    Stopwatch stopwatch = new Stopwatch();
 6             //    stopwatch.Start();
 7             //    Thread.Sleep(2000);
 8             //    stopwatch.Stop();
 9             //    Console.WriteLine(stopwatch.ElapsedMilliseconds);
10             //}
11             {
12                 //Stopwatch stopwatch = new Stopwatch();
13                 //stopwatch.Start();
14                 //Task.Delay(2000).ContinueWith(t =>
15                 //{
16                 //    stopwatch.Stop();
17                 //    Console.WriteLine(stopwatch.ElapsedMilliseconds);
18                 //});
19             }

 

转载于:https://www.cnblogs.com/Spinoza/p/10982053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值