C#编程之c# task
小标 2019-01-15 来源 : 阅读 5896 评论 0

摘要:本文主要向大家介绍C#编程之c# task了,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍C#编程之c# task了,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

Task 与Thread, lambda,  action,funct之间有什么关联。

Task相比Thread提供了什么方便之处。

Thread

1. 创建费时间,占用内存与CPU的资源

For example, when a new Thread is started in .NET, there is a whole process that goes with that, such as creating queues, thread local storage, managing the Thread's lifecycle etc. This takes time

ThreadPool

1. ThreadPool 免去了创建thread的时间,用户直接将work item 委托给ThreadPool去安排其内部的线程调用。

2. 但classic threading ThreadPool,当一个work item已经在执行当中,用户不能去直接关闭,想得到其的结果也是很直接。

However, even using the classic threading ThreadPool, there were problems in that you could not cancel a work item once it has been queued with the ThreadPool, or get a return result that easily. It just doesn't read that well either. There is an excellent article here on CodeProject that tackles some of these issues: Smart ThreadPool, which is pretty excellent actually. However, the new TPL infrastructure has got all these problems covered, and many many more useful features in my opinion.

Task

1. the new TPL infrastructure has got all these problems covered, and many many more useful features in my opinion

Task 继承了ThreadPool的优点,解决了ThreadPool的问题。其本质也可以讲Smart ThreadPool

2. A TPL Task actually uses the ThreadPool internally.

 

Task, ThreadPool, Thread, Scheduler

It is worth mentioning that Tasks are merely wrappers for passing a delegate of work to be done, also storing state, exceptions, and continuations amongst others. That work may or may not be done by the threadpool, and as already stated, that will depend upon the scheduler used.

 

常用的使用场景

Tasks also seem to be more inline with how people think about things. For instance, imagine this scenario: "I want to call a Web Service and have it return a List<int>". Using a TPL Task, I would create a Task<List<int>> and get it to call some service in its payload delegate (which will use the ThreadPool) that returned me a List<int>.

 

Task与UI Thread

reate/cancel Tasks and handle Exceptions

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 8 不喜欢 | 11
看完这篇文章有何感觉?已经有19人表态,42%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程