What is the use of AsyncCallback and why should we use it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When the
asyncmethod finish the processing,AsyncCallbackmethod is automatically called, where post processing statements can be executed. With this technique there is no need to poll or wait for theasyncthread to complete.Here’s some more explanation on
AsyncCallback usage:Callback Model: The callback model requires that we specify a method to callback on and include any state that we need in the callback method to complete the call. The callback model can be seen in the following example:
In this model, we are creating a new
AsyncCallbackdelegate, specifying a method to call (on another thread) when the operation is complete. Additionally, we are specifying some object that we might need as the state of the call. For this example, we are sending the stream object in because we will need to callEndReadand close the stream.The method that we create to be called at the end of the call would look something like this:
Other techniques are Wait-until-done and Polling.
Wait-Until-Done Model The wait-until-done model allows you to start the asynchronous call and perform other work. Once the other work is done, you can attempt to end the call and it will block until the asynchronous call is complete.
Or you can use wait handles.
Polling Model The polling method is similar, with the exception that the code will poll the
IAsyncResultto see whether it has completed.