C # (synchronous calls, asynchronous calls, asynchronous callback) C # (synchronous calls, asynchronous calls, asynchronous callbacks)

C # (synchronous calls, asynchronous calls, asynchronous callbacks)

 

Review:

Although the original author uses the class name of Chinese characters, it looks very bad, but still the synchronous calls, asynchronous calls, using asynchronous callback to explain in great detail. Principle on the very clear.

------

 

This article will explain in a moment to entrust with the implementation of a "class addition" of the differences and the pros and cons through three examples "synchronous call", "asynchronous call", "asynchronous callbacks."
 

First, the following three examples and methods to be invoked by a delegate code defines:


    int the AddHandler the delegate public (A int, int B);
    public class adder class
    {
        public static int the Add (A int, int B)
        {
            Console.WriteLine ( "Start calculation:" A + + "+" + B);
            the Thread. Sleep (3000); // the simulation method for operating three seconds
            Console.WriteLine ( "calculation is done!");
            return A + B;
        }
    }

 

 

Synchronous call

Invoke method used for the commission of a synchronous call. Synchronous call can also be called a blocking call, it blocks the current thread, then make the call, the call is completed and then continue down.

public class synchronous call
{
        static void the Main ()
        {
            Console.WriteLine ( "================ ================ synchronous call SyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);
            int Result = handler.Invoke (1, 2);

            Console.WriteLine ( "keep doing something else ...");

            Console.WriteLine (the Result);
            Console.ReadKey ();
        }
        
}

Synchronous call will block the thread, if it is to be called a lot of work (such as a large number of IO operations), may make the program pause for a long time, resulting in a poor user experience, this time an asynchronous call is very necessary.
 

 

Asynchronous call

Asynchronous call does not block the thread, but to call the stuffed thread pool, the program's main UI thread or threads can proceed.
Asynchronous delegate invocation is achieved by BeginInvoke and EndInvoke.
 

public class asynchronous call
{
        static void the Main ()
        {
            Console.WriteLine ( "asynchronous calls ================ ================ AsyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);

            // the IAsyncResult: asynchronous operation Interface (interface)
            // BeginInvoke: Begins an asynchronous method delegate (delegate) of
            IAsyncResult the Result = handler.BeginInvoke (1, 2, null, null);

            Console.WriteLine ( "keep doing something else ...");

            // asynchronous operation returns
            Console.WriteLine (handler.EndInvoke (Result));
            the Console.ReadKey ();
        }
        
}

We can see, the main thread did not wait, but directly run down.
But the problem still exists, when the main thread running to EndInvoke, if the time is not the end of the call (this situation is likely to occur), then to wait for the result of the call, the thread will still be blocked.
 

 Asynchronous delegates, reference may be written as follows:

Action<object> action=(obj)=>method(obj);
action.BeginInvoke(obj,ar=>action.EndInvoke(ar),null);

Simple sentence or two to complete an operation.
 

 

Asynchronous callbacks

Using a callback function, when the end of the call will automatically call the callback function to solve the wait for the result of the call, and let the thread is still blocked situation.

public class asynchronous callback
{
        static void the Main ()
        {
            Console.WriteLine ( "asynchronous callback ================ ================ AsyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);

            // asynchronous operation Interface ( ! Note BeginInvoke different methods)
            IAsyncResult the Result = handler.BeginInvoke (1, 2, new new AsyncCallback (callback function), "AsycState: the OK");
            
            Console.WriteLine ( "keep doing something else ...");
            Console. ReadKey ();
        }

        static void callback (IAsyncResult Result)
        {// Result is "addition type .Add () method" return value

            // AsyncResult is an implementation IAsyncResult interface space: System.Runtime.Remoting.Messaging
            // AsyncDelegate properties can be coerced to delegate actual user-defined classes.
            Handler = the AddHandler (the AddHandler) ((the AsyncResult) Result) .AsyncDelegate;
            Console.WriteLine (handler.EndInvoke (Result));
            Console.WriteLine (result.AsyncState);
        }
        
}

I define the type of commission is AddHandler, then in order to access AddHandler.EndInvoke, asynchronous delegate must be cast to AddHandler. MAddHandler.EndInvoke can call in an asynchronous callback function (of type AsyncCallback) in order to obtain AddHandler.BeginInvoke results originally submitted. 

 

problem:
 

(1) int result = handler.Invoke ( 1,2);
Why Invoke parameters and return values and AddHandler commission is the same?
A: The
parameter of Invoke method is simple, a commission, a parameter table (optional), and the main function is to help the Invoke method delegate method specified by your call on the UI thread. Invoke method first checks the thread (ie, the current thread) is not the calling UI thread, and if so, direct execution method delegate pointed, if not, it will switch to the UI thread, then execution method delegate pointed . Whether the current thread is not the UI thread, Invoke method will block until the commission completes points, and then switch back to the calling thread (if necessary), return.
So Invoke method parameters and return values and call his commission should be consistent.

(2) Result IAsyncResult handler.BeginInvoke = (1,2, null, null);

BeginInvoke: an asynchronous start request, the calling thread pool to execute a thread,
returned IAsyncResult object (asynchronous core) IAsyncResult simply. an interface status information he stored asynchronous operation, you can also use it to end the current asynchronous.
Note: BeginInvoke and EndInvoke pairs must be called even if no return value, but still must call EndInvoke, as this may cause a memory leak.

 

(3) IAsyncResult.AsyncState properties:
obtaining a user-defined objects, which defines or comprises information about the asynchronous operation. E.g:

static void AddComplete(IAsyncResult result) 
{   
      AddHandler handler = (AddHandler)result.AsyncState;    
      Console.WriteLine(handler.EndInvoke(result)); 
      。。。。。
}

 

Review:

Although the original author uses the class name of Chinese characters, it looks very bad, but still the synchronous calls, asynchronous calls, using asynchronous callback to explain in great detail. Principle on the very clear.

------

 

This article will explain in a moment to entrust with the implementation of a "class addition" of the differences and the pros and cons through three examples "synchronous call", "asynchronous call", "asynchronous callbacks."
 

First, the following three examples and methods to be invoked by a delegate code defines:


    int the AddHandler the delegate public (A int, int B);
    public class adder class
    {
        public static int the Add (A int, int B)
        {
            Console.WriteLine ( "Start calculation:" A + + "+" + B);
            the Thread. Sleep (3000); // the simulation method for operating three seconds
            Console.WriteLine ( "calculation is done!");
            return A + B;
        }
    }

 

 

Synchronous call

Invoke method used for the commission of a synchronous call. Synchronous call can also be called a blocking call, it blocks the current thread, then make the call, the call is completed and then continue down.

public class synchronous call
{
        static void the Main ()
        {
            Console.WriteLine ( "================ ================ synchronous call SyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);
            int Result = handler.Invoke (1, 2);

            Console.WriteLine ( "keep doing something else ...");

            Console.WriteLine (the Result);
            Console.ReadKey ();
        }
        
}

Synchronous call will block the thread, if it is to be called a lot of work (such as a large number of IO operations), may make the program pause for a long time, resulting in a poor user experience, this time an asynchronous call is very necessary.
 

 

Asynchronous call

Asynchronous call does not block the thread, but to call the stuffed thread pool, the program's main UI thread or threads can proceed.
Asynchronous delegate invocation is achieved by BeginInvoke and EndInvoke.
 

public class asynchronous call
{
        static void the Main ()
        {
            Console.WriteLine ( "asynchronous calls ================ ================ AsyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);

            // the IAsyncResult: asynchronous operation Interface (interface)
            // BeginInvoke: Begins an asynchronous method delegate (delegate) of
            IAsyncResult the Result = handler.BeginInvoke (1, 2, null, null);

            Console.WriteLine ( "keep doing something else ...");

            // asynchronous operation returns
            Console.WriteLine (handler.EndInvoke (Result));
            the Console.ReadKey ();
        }
        
}

We can see, the main thread did not wait, but directly run down.
But the problem still exists, when the main thread running to EndInvoke, if the time is not the end of the call (this situation is likely to occur), then to wait for the result of the call, the thread will still be blocked.
 

 Asynchronous delegates, reference may be written as follows:

Action<object> action=(obj)=>method(obj);
action.BeginInvoke(obj,ar=>action.EndInvoke(ar),null);

Simple sentence or two to complete an operation.
 

 

Asynchronous callbacks

Using a callback function, when the end of the call will automatically call the callback function to solve the wait for the result of the call, and let the thread is still blocked situation.

public class asynchronous callback
{
        static void the Main ()
        {
            Console.WriteLine ( "asynchronous callback ================ ================ AsyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);

            // asynchronous operation Interface ( ! Note BeginInvoke different methods)
            IAsyncResult the Result = handler.BeginInvoke (1, 2, new new AsyncCallback (callback function), "AsycState: the OK");
            
            Console.WriteLine ( "keep doing something else ...");
            Console. ReadKey ();
        }

        static void callback (IAsyncResult Result)
        {// Result is "addition type .Add () method" return value

            // AsyncResult is an implementation IAsyncResult interface space: System.Runtime.Remoting.Messaging
            // AsyncDelegate properties can be coerced to delegate actual user-defined classes.
            Handler = the AddHandler (the AddHandler) ((the AsyncResult) Result) .AsyncDelegate;
            Console.WriteLine (handler.EndInvoke (Result));
            Console.WriteLine (result.AsyncState);
        }
        
}

I define the type of commission is AddHandler, then in order to access AddHandler.EndInvoke, asynchronous delegate must be cast to AddHandler. MAddHandler.EndInvoke can call in an asynchronous callback function (of type AsyncCallback) in order to obtain AddHandler.BeginInvoke results originally submitted. 

 

problem:
 

(1) int result = handler.Invoke ( 1,2);
Why Invoke parameters and return values and AddHandler commission is the same?
A: The
parameter of Invoke method is simple, a commission, a parameter table (optional), and the main function is to help the Invoke method delegate method specified by your call on the UI thread. Invoke method first checks the thread (ie, the current thread) is not the calling UI thread, and if so, direct execution method delegate pointed, if not, it will switch to the UI thread, then execution method delegate pointed . Whether the current thread is not the UI thread, Invoke method will block until the commission completes points, and then switch back to the calling thread (if necessary), return.
So Invoke method parameters and return values and call his commission should be consistent.

(2) Result IAsyncResult handler.BeginInvoke = (1,2, null, null);

BeginInvoke: an asynchronous start request, the calling thread pool to execute a thread,
returned IAsyncResult object (asynchronous core) IAsyncResult simply. an interface status information he stored asynchronous operation, you can also use it to end the current asynchronous.
Note: BeginInvoke and EndInvoke pairs must be called even if no return value, but still must call EndInvoke, as this may cause a memory leak.

 

(3) IAsyncResult.AsyncState properties:
obtaining a user-defined objects, which defines or comprises information about the asynchronous operation. E.g:

static void AddComplete(IAsyncResult result) 
{   
      AddHandler handler = (AddHandler)result.AsyncState;    
      Console.WriteLine(handler.EndInvoke(result)); 
      。。。。。
}

 

Review:

Although the original author uses the class name of Chinese characters, it looks very bad, but still the synchronous calls, asynchronous calls, using asynchronous callback to explain in great detail. Principle on the very clear.

------

 

This article will explain in a moment to entrust with the implementation of a "class addition" of the differences and the pros and cons through three examples "synchronous call", "asynchronous call", "asynchronous callbacks."
 

First, the following three examples and methods to be invoked by a delegate code defines:


    int the AddHandler the delegate public (A int, int B);
    public class adder class
    {
        public static int the Add (A int, int B)
        {
            Console.WriteLine ( "Start calculation:" A + + "+" + B);
            the Thread. Sleep (3000); // the simulation method for operating three seconds
            Console.WriteLine ( "calculation is done!");
            return A + B;
        }
    }

 

 

Synchronous call

Invoke method used for the commission of a synchronous call. Synchronous call can also be called a blocking call, it blocks the current thread, then make the call, the call is completed and then continue down.

public class synchronous call
{
        static void the Main ()
        {
            Console.WriteLine ( "================ ================ synchronous call SyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);
            int Result = handler.Invoke (1, 2);

            Console.WriteLine ( "keep doing something else ...");

            Console.WriteLine (the Result);
            Console.ReadKey ();
        }
        
}

Synchronous call will block the thread, if it is to be called a lot of work (such as a large number of IO operations), may make the program pause for a long time, resulting in a poor user experience, this time an asynchronous call is very necessary.
 

 

Asynchronous call

Asynchronous call does not block the thread, but to call the stuffed thread pool, the program's main UI thread or threads can proceed.
Asynchronous delegate invocation is achieved by BeginInvoke and EndInvoke.
 

public class asynchronous call
{
        static void the Main ()
        {
            Console.WriteLine ( "asynchronous calls ================ ================ AsyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);

            // the IAsyncResult: asynchronous operation Interface (interface)
            // BeginInvoke: Begins an asynchronous method delegate (delegate) of
            IAsyncResult the Result = handler.BeginInvoke (1, 2, null, null);

            Console.WriteLine ( "keep doing something else ...");

            // asynchronous operation returns
            Console.WriteLine (handler.EndInvoke (Result));
            the Console.ReadKey ();
        }
        
}

We can see, the main thread did not wait, but directly run down.
But the problem still exists, when the main thread running to EndInvoke, if the time is not the end of the call (this situation is likely to occur), then to wait for the result of the call, the thread will still be blocked.
 

 Asynchronous delegates, reference may be written as follows:

Action<object> action=(obj)=>method(obj);
action.BeginInvoke(obj,ar=>action.EndInvoke(ar),null);

Simple sentence or two to complete an operation.
 

 

Asynchronous callbacks

Using a callback function, when the end of the call will automatically call the callback function to solve the wait for the result of the call, and let the thread is still blocked situation.

public class asynchronous callback
{
        static void the Main ()
        {
            Console.WriteLine ( "asynchronous callback ================ ================ AsyncInvokeTest");
            the AddHandler the AddHandler Handler = new new (class adder .Add);

            // asynchronous operation Interface ( ! Note BeginInvoke different methods)
            IAsyncResult the Result = handler.BeginInvoke (1, 2, new new AsyncCallback (callback function), "AsycState: the OK");
            
            Console.WriteLine ( "keep doing something else ...");
            Console. ReadKey ();
        }

        static void callback (IAsyncResult Result)
        {// Result is "addition type .Add () method" return value

            // AsyncResult is an implementation IAsyncResult interface space: System.Runtime.Remoting.Messaging
            // AsyncDelegate properties can be coerced to delegate actual user-defined classes.
            AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
            Console.WriteLine(handler.EndInvoke(result));
            Console.WriteLine(result.AsyncState);
        }
        
}

I define the type of commission is AddHandler, then in order to access AddHandler.EndInvoke, asynchronous delegate must be cast to AddHandler. MAddHandler.EndInvoke can call in an asynchronous callback function (of type AsyncCallback) in order to obtain AddHandler.BeginInvoke results originally submitted. 

 

problem:
 

(1) int result = handler.Invoke ( 1,2);
Why Invoke parameters and return values and AddHandler commission is the same?
A: The
parameter of Invoke method is simple, a commission, a parameter table (optional), and the main function is to help the Invoke method delegate method specified by your call on the UI thread. Invoke method first checks the thread (ie, the current thread) is not the calling UI thread, and if so, direct execution method delegate pointed, if not, it will switch to the UI thread, then execution method delegate pointed . Whether the current thread is not the UI thread, Invoke method will block until the commission completes points, and then switch back to the calling thread (if necessary), return.
So Invoke method parameters and return values and call his commission should be consistent.

(2) Result IAsyncResult handler.BeginInvoke = (1,2, null, null);

BeginInvoke: an asynchronous start request, the calling thread pool to execute a thread,
returned IAsyncResult object (asynchronous core) IAsyncResult simply. an interface status information he stored asynchronous operation, you can also use it to end the current asynchronous.
Note: BeginInvoke and EndInvoke pairs must be called even if no return value, but still must call EndInvoke, as this may cause a memory leak.

 

(3) IAsyncResult.AsyncState properties:
obtaining a user-defined objects, which defines or comprises information about the asynchronous operation. E.g:

static void AddComplete(IAsyncResult result) 
{   
      AddHandler handler = (AddHandler)result.AsyncState;    
      Console.WriteLine(handler.EndInvoke(result)); 
      。。。。。
}

 

Guess you like

Origin www.cnblogs.com/gaoxianzhi/p/11946388.html