■ Basic usage of the task class
The task class is a class that improves the Thread class to make it easier to use. Therefore, although the functions are similar, they belong to two concepts and are used differently.
To use the task class to start a task, you only need to call the task. Factory. startnew method.
Task1.cs
Using system; Using system. Threading. tasks; Class Program { Static void main (string [] ARGs) { // Subtask VaR task = task. Factory. startnew () => { For (INT I = 0; I <100; I ++) console. Write ('B '); }); // Director For (INT I = 0; I <100; I ++) console. Write ('A '); Task. Wait (); } } |
Running result (the result varies with the environment and time series)
AAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB |
The Director outputs a and the sub-task outputs B. Because the two tasks are executed in parallel, the two outputs are executed at the same time, and the sequence of output results is uncertain.
The last wait method is to wait for the task to end. In this way, you can ensure that the sub-task ends before exiting the program.
■ Do this after preparation
It is common to specify the code to be executed when a condition is met.
It is more often said that "when something is done, I want to do it next ". If you use the task class, this idea is easy to implement.
Task2.cs
class Program { private static void notify(Task t) { Console.WriteLine(); Console.WriteLine("sub task {0} done", t.Id); } static void Main(string[] args) { var task = Task.Factory.StartNew(() => { for (int i = 0; i < 100; i++) Console.Write('B'); }); task.ContinueWith(notify); for (int i = 0; i < 100; i++) Console.Write('A'); task.Wait(); } } |
Running result
BBBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB sub task 1 done |
As shown in the example, you only need to use the continuewith method. Of course, the code to be executed can also be written using lambda expressions. In this way, you can execute the continuewith method after the specified task ends. At the same time, the continuewith method can be used for the same task object for multiple times. Of course, the method is executed in the specified sequence.
In addition, if the notify method is rewritten using a Lambda expression, the call of the continuewith method is shown in the following example.
Rewrite the y method using lambda expressions
task.ContinueWith((t) => { Console.WriteLine(); Console.WriteLine("sub task {0} done", t.Id); }); |
Here, the parameter t of the lambda expression carries the same content as the variable task. In other words, the value is the same. In this way, when calling method M, like method X. M (x), we also use the form of "X", which is called "explicit this pointer ". "Explicit this pointer" is frequently used in programs. Although the same content in the same row is defined by two names (task and T in this example), if Lambda expressions are used, you can reference the variable task in the lambda expression.