C#Beginning Asynchronous

What is asynchronous

Take an example: Xiaoming's mother asked Xiaoming to boil a jug of water. When the water boils, it pours into the jug. At the same time, she needs to clean the house.

Xiao Ming's operation procedure 1: Boil water --> Wait until the water boils ---> Pour water into the kettle ------> Clean up household chores

Xiao Ming's operation procedure 2: boiling water----->cleaning household work--------> Pour water into the kettle when the water is boiled------> Continue cleaning household work.

The second way is asynchronous.

 

Asynchronous use

1. Call asynchronous by delegation

  delegate void Write();

        static void Main(string[] args)
        {
            Console.WriteLine("I am the main thread");

            Write write = new Write(new Program().WriteLine);
            write.BeginInvoke(new Program().Judge, null);
            Console.WriteLine("I am still the main thread");
            Console.Read();
        }

        private void WriteLine() {
            Console.WriteLine("Start calculating 1+1");
            Thread.Sleep(3000);
            Console.WriteLine("The result is 3!");
        }

        private void Judge(IAsyncResult async) {
            Console.WriteLine("Calculation error!");
        }

Run Results; where WriteLine() is the method that requires asynchronous execution, Judge() is the method that is called after asynchronous execution, and if there is no callback, change the new Program().Judge to null when executing delegation.

 

2. async, await

 

Asynchronous methods need to be tagged with the async keyword, but async tagged methods are not necessarily asynchronous methods.

  

  static void Main(string[] args)
        {
            Console.WriteLine("I am the main thread 1");
            Task task =new Program(). Write1();
            Console.WriteLine("I am Main Thread 2");
            Console.Read();
        }

        private async Task Write1() 
        {
            int po = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine(po);
            Console.WriteLine("First async Run End");
        }

 

Run result,, you can see that Write1() is still executing in the main thread.

 

Limitations of await use: must be used in methods marked with async. The return value type of subsequent methods must be Task or Task<T>..

 

  static void Main(string[] args)
        {
            Console.WriteLine("I am the main thread 1");
            Task task =new Program(). Write1();
            Console.WriteLine("I am Main Thread 2");
            Console.Read();
        }
      
        private async Task Write1() 
        {
            await Write2();
            int po = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine(po);
            Console.WriteLine("First async Run End");
        }
        private async Task Write2()
        { 
            Console.WriteLine("Hello World!");
            await Write3();
            for (int i = 0; i < 3; i++)
            {   
                int po = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine(po);
                Console.WriteLine("Working..{0}", i);
                await Task.Delay(1000); //and Thread.Sleep(1000)The effect is the same.
            }
        }

        private async Task Write3() {
           
            for (int i = 0; i < 3; i++)
            {
                int po = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine(po);
                Console.WriteLine("wirte3");
                await Task.Delay(1000);
            }
        }

 

 

 

Run Results

Await represents asynchronous after await XX(), and the XX() method is still executed in the main thread, so in Write2()

Console.WriteLine("Hello World!") is completed in the main thread, and the first loop of Write3() is also completed in the main thread, and then there is one after the first end
 await Task.Delay(1000); so the second loop is done in an asynchronous thread.

Tags: C#

Posted on Sun, 25 Aug 2019 19:47:05 -0700 by tllewellyn