Thread Task Pritce

Source: Internet
Author: User

1. Use the task class to create and perform simple tasks :

Use the constructor of the task to create the task and call the Start method to start the task and perform an asynchronous operation

Aitall is used to wait for all System.Threading.Tasks.Task objects provided to complete the execution process and wait for any of the supplied

There are 2 other ways to wait, one is WaitAll, the other is Waitnay.

Wait for all tasks to complete    void WaitAll (params task[] tasks);  Wait for any task to complete int WaitAny (params task[] tasks);    

2. wait for the task to complete and get the return value

When using a task to perform an asynchronous operation, it is important to get the return value after the asynchronous operation is complete

The wait method can wait for the task to finish executing.

The code is as follows: Outputs 1 to 3 of the sum of the numbers

The results are as follows:

3. use ContinueWith to start a new task when the task is completed


When the wait method waits for a task, it is possible to block the thread, and in order to solve this problem, we can use the ContinueWith method
He can automatically start a new task to process the execution results when a task is completed.

Instead of waiting for completion to display the result of the execution, the example above uses the ContinueWith method, which knows when the task finishes and starts a new task to perform the task after the completion of the process.

4. Creating parent-child tasks and task factory usage

tasks created through the task class are top-level tasks that can be accomplished by using the Taskcreationoptions.attachedtoparent
The identity associates these tasks with the task of creating them, and the parent task does not end until all subtasks are complete

The results are as follows:

Thread Task Pritce

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.