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