C #4.0 Chapter 3 Task class and Plinq-page 1 Task class

Source: Internet
Author: User

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.

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.