Use of C # task---start of task

Source: Internet
Author: User

. NET 4.0 contains the new namespace System.Threading.Tasks, which contains classes that abstract threading functionality. A task represents the work of a unit that should be completed. The work of this unit can be run in a separate thread, or it can start a task synchronously, which needs to wait for the thread to be activated. Using a task not only gives you an abstraction layer, but also gives you a lot of control over the underlying thread.

Start a task

1), using an instance of the TaskFactory class, in which the Taskmethod () method is passed to the StartNew method, the task is started immediately.

   Using System;
   Using System.Collections.Generic;
   Using System.Linq;
   Using System.Text;
   Using System.Threading;
   Using System.Threading.Tasks;
   Namespace Tasksamples
   8: {
   9:     class Program
  Ten:     {
  One:         void Taskmethod ()
  :         {
  :             Console.WriteLine ("Running in a task");
  :             Console.WriteLine ("Task id:{0}", Task.currentid);
  :         }
  
  +:         void Main (string[] args) 
  :         {
  :             //using Task Factory
  :             new TaskFactory ();
  :             Task t1 = tf. StartNew (Taskmethod);
  :             Console.readkey ();
  :          }
  :     }
  25:}

The results of the operation are as follows:

2), using the Factory property of the Task class, Task.factory returns the default instance of TaskFactory. In which the Taskmethod () method is passed to the StartNew method. This method is actually the same as the first method.

   Using System;
   Using System.Collections.Generic;
   Using System.Linq;
   Using System.Text;
   Using System.Threading;
   Using System.Threading.Tasks;
   Namespace Tasksamples
   8: {
   9:     class Program
  Ten:     {
  One:         void Taskmethod ()
  :         {
  :             Console.WriteLine ("Running in a task");
  :             Console.WriteLine ("Task id:{0}", Task.currentid);
  
  17:         
  :         }
  :         void Main (string[] args) 
  :         {
  20:            
  £             //using the task factory via a task
  :             Task t2 = Task.Factory.StartNew (Taskmethod);
  At:             Console.readkey ();
  :          }
  :     }
  26:}

3), use an instance of the task class, and then call the Start method to start the task. The result of the operation is the same as the above two methods. When using the task class, you can also call the runsynchronously () method In addition to the start () method. The task will also start, but the caller will have to wait until the end of the task while it is running in the caller's current thread. By default, tasks are run asynchronously.

   Using System;
   Using System.Collections.Generic;
   Using System.Linq;
   Using System.Text;
   Using System.Threading;
   Using System.Threading.Tasks;
   Namespace Tasksamples
   8: {
   9:     class Program
  Ten:     {
  One:         void Taskmethod ()
  :         {
  :             Console.WriteLine ("Running in a task");
  :             Console.WriteLine ("Task id:{0}", Task.currentid);
  :         }   
  16:              
  +:         void Main (string[] args) 
  :         {             
  :             ////using Task Constructor
  :             New Task (Taskmethod);
  £             T3. Start ();
  :             Console.readkey ();
  :          }
  :     }
  25:}

When you use the constructor of the task class and the StartNew () method of the TaskFactory class, you can pass the value in the TaskCreationOptions enumeration. Set the longrunning option to notify the Task Scheduler that the task takes a long time to execute, so that the scheduler is more likely to use the new thread. If the task should be associated to a parent task and the parent task is canceled, the task should also be canceled, and the attachtoparent option should be set. A value of perferfairness indicates that the scheduler should extract the first task that is already waiting. If a task is created in another task, this is not the default. If a task uses subtasks to create other work, the subtasks take precedence over other tasks. They do not end up in the queue of the online pool. If these tasks should be handled in a fair manner with all other tasks, set this option to perferfairness.

   New Task (taskmethod,taskcreationoptions.preferfairness);
   2:           T3. Start ();

Http://www.cnblogs.com/jerry01/archive/2012/09/14/2684914.html

Use of C # task---start of task

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.