. NET parallel (multicore) Programming series six Task Foundation part end

Source: Internet
Author: User

Preface: The previous article introduced some of the basic parallel programming, but also focused on some of the use of task, this is very short, will end the task of the introduction of the basics.

The main topics of this article are as follows:

1. Get the status of a task

2. Perform a late-loading task (lazily task)

3. Solutions to common problems

1. Get the status of a task

One of the more standardized operations in. NET parallel programming is to get the state of the task, which is obtained by the Task.status property, which returns an System.Threading.Tasks.TaskStatus enumeration value.

As follows:

Created: Indicates that the task has been initialized, but has not been added to the scheduler.

Watingforactivation:task is waiting to be added to the scheduler.

Waitingtorun: Has been added to the scheduler, waiting to be executed.

Running:task is running

Waitingforchildrentocomplete: Indicates that the parent task is waiting for the child task to finish running.

Rantocompletion: The task has been executed, but has not been cancel, and this task has not thrown an exception.

Canceled: It means the task has been cancel. (You can refer to the previous article that tells you to cancel the task)

Faulted: Indicates that the task has thrown an exception when it is running.

2. Perform a late-loading task (lazily task)

Late loading, or name delay initialization, the main benefit is to avoid unnecessary system overhead. In parallel programming, lazy variables and task<> can be used in conjunction;. Factory.startnew () to do this. (lazy variable is a new feature in. NET 4, where you don't need to know the specifics of lazy)

Lazy variables are initialized only when they are used. So we can combine the lazy variable with the creation of a task: The task is initialized only when it is executed.

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.