C # Implementation of asynchronous programming (4) -- Task

Source: Internet
Author: User

. Net Framework 4.0 adds a System. Threading. Tasks namespace, which contains classes that provide task-related operations. By using tasks, you can not only obtain an abstraction layer, but also perform many unified control operations on the underlying threads. Here we will first introduce the simple use of tasks.

The most basic thing is to know how to start a Task.

 

1. Task Constructor

Use the constructor of the Task class. When instantiating a Task object, the Task does not run immediately, but specifies the Created state. Call the Start () method of the Task class to Start the Task. When using the Task class, you can call the RunSynchronously () method in addition to the Start () method. In this way, the task will also be started but called at the same time. By default, tasks run asynchronously.

The constructor of the Task class receives a delegate with no parameters and no return values:

   1: Task task = new Task(TaskMethod);
   2: task.Start();

The following is the TaskMethod method:

   1: static void TaskMethod()
   2: {
   3:     for (int i = 0; i < 10; i++)
   4:     {
   5:         Console.WriteLine(String.Format("Running in a task. Task ID: {0}", Task.CurrentId));
   6:         Thread.Sleep(500);
   7:     }
   8: }

Use the Task. CurrentId attribute to obtain the current Task ID. The main thread is as follows:

   1: static void Main(string[] args)
   2: {
   3:     Task task = new Task(TaskMethod);
   4:     task.Start();
   5:  
   6:     for (int i = 0; i < 10; i++)
   7:     {
   8:         Console.WriteLine("Running in main thread.");
   9:         Thread.Sleep(500);
  10:     }
  11:     
  12:     Console.Read();
  13: }

If you want to pass parameters to the thread, the Task constructor provides a heavy load, you can pass in an object-type parameter:

   1: Task task = new Task(TaskMethodWithParameter, "Hello world");
   2: task.Start();

The following is a thread method with parameters:

   1: static void TaskMethodWithParameter(object param)
   2: {
   3:     for (int i = 0; i < 10; i++)
   4:     {
   5:         Console.WriteLine(String.Format("Running in a task. Parameter: {0}", param));
   6:         Thread.Sleep(500);
   7:     }
   8: }

 

 

 

2. TaskFactory class

Use the instantiated TaskFactory class to pass the TaskMethod method to the StartNew () method, and the task is started immediately.

   1: TaskFactory tf = new TaskFactory();
   2: tf.StartNew(TaskMethod);

 

 

 

3. Properties of Task. Factory

The Task class provides a Factory static attribute, which returns a TaskFactory object.

   1: Task task = Task.Factory.StartNew(TaskMethod);

 

 

 

Subsequent articles will introduce continuous tasks and hierarchical tasks in tasks.

References: C # advanced programming, http://developer.51cto.com/art/200908/145541.htm

 

Http://www.cnblogs.com/sosowjb/archive/2012/08/11/2633953.html

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.