C # basics: asynchronous thread of callback (delegate)

ylbtech-C # basics: the thread of the asynchronous callback (delegate)

 

1. Back to top
1、

Asynchronous callback, what is an asynchronous callback? I understand it this way, when the main thread while executing a piece of code, we entrust the implementation of a thread, the thread to return a result, the key is when to return this result, asynchronous callback is to return this immediately after the thread execution is completed the results of threads, and then continue with main thread.

      Before discussing the asynchronous callback Let's turn to discuss the implementation of synchronous and asynchronous execution, and finally to discuss asynchronous callbacks.

First, synchronous execution

      Synchronous execution: When the main thread of execution, a main thread calls the other methods, this time the main thread blocking, call waiting thread can not complete the main method of execution to proceed.

  The following is a flowchart executed in synchronization

  Then take a look synchronization code execution

Copy the code
        the Main void static (String [] args) 
        { 
            #region synchronous execution 
            for (int I = 0; I <100; I ++) 
            { 
                IF (I == 50) 
                { 
                    Console.Write ( "call and wait method TakeAWhile method execution is completed. "); 
                    TakeAWhile (. 1, 3000); 
                    Console.Write (" method TakeAWhile execution is complete ... "); 
                } 
                the Thread.Sleep (30); 
                Console.Write (" ");. 
            } 
            #endregion 
        } 
        static int TakeAWhile ( Data int, int MS) 
        { 
            the Thread.Sleep (MS);  
            return ++ Data;
        }
Copy the code

Second, asynchronous execution:

  Asynchronous execution: When the main thread in execution, to open a sub-thread, the main thread continues to execute when the main thread results need to sub-thread running when the main thread directly call the child thread running result, the time if the call of the child thread has not execution is completed, the main thread to wait until the child thread execution is completed, the main thread to continue execution.

  The following is a flowchart executed asynchronously

  Next we look at the code for asynchronous execution

Copy the code
        static void Main(string[] args)
        {
            #region 异步执行
            Func<int, int, int> d2 = TakeAWhile;
            IAsyncResult ar = d2.BeginInvoke(1, 3000, null, null);
            while (!ar.IsCompleted)
            {
                Console.Write(".");
                Thread.Sleep(50);
            }
            int result = d2.EndInvoke(ar);
            Console.Write("result:{0}",result);
            #endregion
        }
        static int TakeAWhile(int data, int ms)
        {
            Console.Write("TakesAWhile started");
            Thread.Sleep(ms);
            Console.WriteLine("TakeAWhile Completed");
            return ++data;
        }
Copy the code

三、异步回调

         异步回调:主线程在执行的时候,打开一个子线程,主线程继续执行,当子线程执行完成的时候,主线程立即输出主线程的运行结果,主线程继续执行。

         下面是异步回调执行的流程图

接下来我们看看代码中的异步回调

Copy the code
static void Main(string[] args)
        {
            #region 异步回调
            Func<int, int, int> dl = TakeAWhile;
            dl.BeginInvoke(1, 3000, TakesAWhileCompleted, dl);
            for (int i = 0; i < 100; i++)
            {
                Console.Write(".");
                Thread.Sleep(50);
            }
            #endregion
        }
        static int TakeAWhile(int data, int ms)
        {
            Console.Write("TakesAWhile started");
            Thread.Sleep(ms);
            Console.WriteLine("TakeAWhile Completed");
            return ++data;
        }
        static void TakesAWhileCompleted(IAsyncResult ar)
        {
            if (ar == null)
                throw new ArgumentNullException("ar");
            Func<int, int, int> dl = (Func<int, int, int>)ar.AsyncState;
            int result = dl.EndInvoke(ar);
            Console.WriteLine("Result:{0}",result);
        }
Copy the code

Description: The flow chart is drawn by their own understanding, if wrong, thank you pointed out, to explore learning together.

2、
2. Return to top
 
3. Back to top
 
4. Top
 
5. Top
 
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/12151466.html