C # Task

Source: Internet
Author: User

Task Parallel library:1 of N

Http://www.codeproject.com/Articles/152765/Task-Parallel-Library-of-n

What is the correlation between Task and thread, lambda, action,funct.

Task provides more convenience than thread.

Thread

1. Create fee time, consume memory and CPU resources

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

ThreadPool

1. ThreadPool eliminates the time to create thread, and the user directly delegates the work item to ThreadPool to schedule its internal thread invocation.

2. But classic threading ThreadPool , when a work item is already in the execution, the user can not go directly to shut down, want the result is also very direct.

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

Task

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

The Task inherits the advantages of ThreadPool and solves the ThreadPool problem. The essence can also be said smart ThreadPool

2. A TPL Task actually uses the ThreadPool internally.

Task, ThreadPool, Thread, Scheduler

It is worth mentioning that Task S was merely wrappers for passing a delegate of work to being done, also storing state, exce Ptions, and continuations amongst others. That work could or May is not being done by the ThreadPool, and as already stated, which would depend upon the scheduler used.

Common usage scenarios

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

Task and UI Thread

C # Task

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.