A brief introduction of the Task class

I. Introduction

The Task class is typically performed in an asynchronous manner, asynchronous mode is based on the object Task task into the .NET Framework 4.

Mandate instantiation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyTask
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<object> action = (object obj) =>
            {
                Thread.Sleep(1000);
                Console.WriteLine("obj:{0}", Obj); 
            }; 
            the Task T1 = new new the Task (Action, " task with arguments " ); 
            t1.Start (); 
           
            the Console.ReadKey (); 
        } 
    } 
}

Examples of the above-described task by calling task t1 class constructor instantiated by calling Start () method to perform tasks; it performs Action <object> named delegate the action, so that it can accept the object type of the variable.

III. Creating and executing tasks

Task may be created in different ways instance, our most commonly used method is to call the static method Run, Run method provides a simple way to start the task.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyTask
{
    class Program
    {
        static void Main(string[] args)
        {
            Task.Run(() =>
                {
                    Thread.Sleep(1000);
                    Console.WriteLine("任务执行中。。。");
                });

            Console.WriteLine ( " main thread execution ... " ); 
            the Console.ReadKey (); 
        } 

      
    } 
}

It can replace the use of static methods TaskFactory.StartNew method; and Task.Factory TaskFactory property returns an object, TaskFactory.StartNew overloaded methods to start the task.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyTask
{
    class Program
    {
        static void Main(string[] args)
        {
            TaskFactory factory = new TaskFactory();
            Task task1 = factory.StartNew(() =>
            {
                Console.WriteLine("任务1执行中。。。");
            }); 
           
            The Task Task2 = Task.Factory.StartNew (() => 
            { 
                Console.WriteLine ( " Task 2 execution ... " ); 
            }); 

            (Console.WriteLine " main thread execution ... " ); 
            the Console.ReadKey (); 
        } 

      
    } 
}

IV. To wait for one or more tasks completed

Tasks typically run asynchronously on the thread, create and start a thread will continue to execute the task, and in some cases, the application needs the calling thread to continue execution at a time when one or more tasks are not finished, if you want to wait to perform these tasks is completed, the application can continue execution, you can wait wait method by calling one or more tasks to complete. Task.Wait () method is unconditionally wait, that as long as the task is not completed, it would have been blocked to wait.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyTask
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主程序开始执行。。。");
            TaskFactory factory = new TaskFactory();
            Task task = factory.StartNew(() =>
            {
                The Thread.Sleep ( 1000 ); 
                Console.WriteLine ( " task execution completion ... " ); 
            }); 
            task.Wait (); 
            Console.WriteLine ( " main thread execution is complete ... " ); 
            the Console.ReadKey () ; 
        } 
      
    } 
}

For more information on the Task class, please visit the official document: https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.task?view=netframework-4.5

Guess you like

Origin www.cnblogs.com/QingYiShouJiuRen/p/11238501.html