Simple to use C # Task of

 

Look at a small example ...

 1         static void Main(string[] args)
 2         {
 3             var task = new Task(() =>
 4             {
 5                 Console.WriteLine("task start....");
 6                 Thread.Sleep(2000);
 7                 Console.WriteLine("task end....");
 8             });
 9             task.Start();
10             Console.WriteLine("main start....");
11             Thread.Sleep(1000);
12             Console.WriteLine("main end....");
13             Console.ReadLine();
14         }

 

 Look at the output, the method can be found in the main method in the task should be run at the same time, what we call asynchronous execution.

 

When we use async keyword, you tell the program that method is asynchronous, we await to wait to use asynchronous methods, look at the code ...

        public async void Async()
        {
            await Task.Run(() => {
                Console.WriteLine("task start..");
                /****/
                Console.WriteLine("task end..");
            });
        }

 

Guess you like

Origin www.cnblogs.com/dotnetnote/p/11515498.html