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.