Overview of Asynchronous Programming:
Asynchronous programming allows a program to call methods on a managed object and continue execution until either the managed object calls a specified callback or until blocking, waiting, or polling for the call is completed. You can implement asynchronous programming by using the following options:
Poll Completed | When you use this option, the program repeatedly polls the IAsyncResult.IsCompleted property of the asynchronous method to check whether the method has finished execution. |
Wait Handles | In this user can wait for the completion without looping and the client can use a time-out to wake. |
Callbacks | Here we can use callbacks to specify the method that is called when the asynchronous method returns. We specify a callback delegate when you begin a call to an asynchronous method |
BeginInvoke, EndInvoke | When you use this option, a client that makes asynchronous calls block execution until the synchronous method completes execution. up periodically |
.NET asynchronous programming consists of two logical parts.
· In the first part, a server object takes the input from the client and begins the asynchronous operation.
· The server can also take a callback delegate from the client that is called when the server returns the asynchronous call.
· In the second part, the server returns the result of the asynchronous operation to the client.
· The server utilizes the IAsyncResult object that it returned to the client to maintain the state of the asynchronous operation.
Three Options for Making Asynchronous Calls
§ Every application is different, and there are scenarios for making asynchronous calls.
§ There are three possible ways.
· Polling for Completion
· Using WaitHandles
· Using Callbacks
Lets cover each of them in detail.
Polling for Completion
§ The IAsyncResult interface that is returned from our BeginDelayedResponse function has an IsCompleted property that can be checked to determine if the request has completed or not.
§ One option you have is to poll this property until it returns a value of True
Dim proxy as New localhost.Service1()
Dim result as IAsyncResult
Result = proxy.BeginDelayedResponse(2000, Nothing, Nothing)
While (result.IsCompleted = False)
' Do some processing
...
Wend
Dim response as String
response = proxy.EndDelayedResponse(result)
Problems –
§ One of the problems when using polling to determine the call completion, is that you can end up using a lot of your machine's CPU cycles if you are not careful.
§ For instance, if there is no code in our while loop, then the thread that is executing this code can gobble up most of the processing resources on your machine.
Advantage -
§ However, if you do have a lot of processing to do and you just want to check once in a while to see if the Web service call is finished, then using polling is not a bad solution.
§ Many times, applications will use a combination of polling with one of the other asynchronous approaches. For instance, Polling with WaitHandles – i.e You make the Web service call asynchronously because you have some background processing to perform, but once you have finished performing your background processing, you may just want to block until the Web service is completed. In this case, you may poll occasionally while you are doing your processing, but then use a WaitHandle to wait for the result once the background processing is complete.
Using WaitHandles
§ WaitHandles are convenient for scenarios where you need to make asynchronous calls, but you do not want to release the thread you are currently executing in. For instance, if you make an asynchronous Web service call from within an ASP.NET application, and then you return from the event you are handling for your ASP.NET application, you may not have the opportunity to include the data from the Web service call in the data returned to the user. With WaitHandles you can do some processing after your Web service call has been made, then block until the Web service call has completed. WaitHandles are particularly useful if you are making multiple Web service calls from within an ASP.NET page.
' Simple WaitHandle code
Dim proxy As New localhost.Service1()
Dim result As IAsyncResult
result = proxy.BeginDelayedResponse(2000, Nothing, Nothing)
' Do some processing.
' ...
' Done processing. Wait for completion.
result.AsyncWaitHandle.WaitOne()
Dim response As String
response = proxy.EndDelayedResponse(result)
Using Callbacks
§ Using callbacks can be very efficient if you are making a lot of simultaneous Web service calls.
§ It lends itself well to doing background processing on the results to Web service calls.
§ However, it is a more complex approach than the other two methods.
§ Using callbacks in a Microsoft Windows® application is a particularly nice approach, as this Prevents' Blocking the message thread for your Window.
Dim proxy as localhost.Service1
Private Delegate Sub MyDelegate(ByVal response As String)
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
proxy = New localhost.Service1()
proxy.BeginDelayedResponse(2000, New AsyncCallback(AddressOf Me.ServiceCallback), Nothing)
End Sub
Private Sub ServiceCallback(ByVal result As IAsyncResult)
Dim response As String
response = proxy.EndDelayedResponse(result)
Label1.Invoke(New MyDelegate(AddressOf Me.DisplayResponse), New Object() {response})
End Sub
Private Sub DisplayResponse(ByVal response As String)
Label1.Text = response
End Sub
Thanks & Regards,
Arun Manglick || Senior Tech Lead
No comments:
Post a Comment