Page view counter

These tutorials demonstrate selected features in ASP.NET version 2.0, but they are compatible with later versions of ASP.NET as well. For the current documentation, see the ASP.NET portal on the MSDN Web site.

 

 

   Welcome   |   ASP.NET   |   Web Services   |   Class Browser   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Make an Asynchronous Call to a Remote Object?

All the examples up to this point made synchronous calls to the remote object. This strategy might not always be desirable since the remote object might have to perform a number of time-consuming tasks and it is not advisable to block the client while a call is in progress. This example demonstrates how to make asynchronous calls.

Reuse the server from the hello sample and modify the client to make asynchronous calls. The following code example demonstrates the new client code.

		
Imports System
Imports System.Threading
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Messaging
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.TCP

Namespace Microsoft.Samples.RemotingSamples
    Public Class Client
        Public Shared e As ManualResetEvent
        Public Delegate Function MyDelegate(name As String) As String

        Shared Sub Main
            e = New ManualResetEvent(false)
            Dim chan As TCPChannel
            chan = New TCPChannel()
            ChannelServices.RegisterChannel(chan)
            dim obj As HelloServer
            obj = CType(Activator.GetObject( _
                Type.GetType("RemotingSamples.HelloServer,Object"), _
                    "tcp://localhost:8085/SayHello"), _
                HelloServer)
            If obj Is Nothing Then
                System.Console.WriteLine("Could not locate server")
            Else
                Dim cb As AsyncCallback
	            cb = New AsyncCallback(AddressOf Client.MyCallBack)
                Dim d As MyDelegate
	            d = New MyDelegate(AddressOf obj.HelloMethod)
                Dim ar As IAsyncResult
                ar = d.BeginInvoke("Caveman", cb, 0)
            End If

            e.WaitOne()
        End Sub

        Shared Sub MyCallBack(ar As IAsyncResult)
            Dim d As MyDelegate = CType(ar, AsyncResult).AsyncDelegate
            Console.WriteLine(d.EndInvoke(ar))
            e.Set()
        End Sub
    End Class
End Namespace
VB

An Event object is used to prevent the client application from returning from Main while the asynchronous call is pending. You need to reset the Event object to false on the first line in Main and wait for state of the object to change before leaving Main. The async programming pattern in the .NET Framework requires a delegate to represent the callback function. You need to declare the delegate that is required before it can be used. Delegates are somewhat similar to function pointers on classes in C++. You have to declare a delegate with the same calling parameters and result type as the method the delegate represents. Since you need to define a delegate for the HelloMethod you want to call, declare the delegate as follows:

		
Public Delegate Function MyDelegate(name As String) As String
VB

The call accepts a string as argument and returns a string as the result. The compiler automatically generates the class MyDelegate when it encounters the declaration and adds a BeginInvoke and EndInvoke method to the delegate that maps to native calls somewhere in the common language runtime. The next is to create the callback function that receives the result from the call.

		
Shared Sub MyCallBack(ar As IAsyncResult)
    Dim d As MyDelegate = CType(ar, AsyncResult).AsyncDelegate
    Console.WriteLine(d.EndInvoke("Caveman", ar))
    e.Set()
End Sub
VB

Notice how the callback declares an object of type IAsyncResult as the parameter of the callback function. Once the call completes, the framework ensures that the result of the call is placed inside the result object and the callback is then invoked back to the client, forwarding the result object to it. To retrieve the result of the call, extract the delegate from the AsyncResult and call EndInvoke. The following code example demonstrates calling the HelloMethod on the remote object.

		
Dim cb As AsyncCallback
cb = New AsyncCallback(AddressOf Client.MyCallBack)
Dim d As MyDelegate
d = New MyDelegate(AddressOf obj.HelloMethod)
Dim ar As IAsyncResult
ar = d.BeginInvoke("Caveman", cb, 0)
VB

Create a delegate for the callback method and one for the remote method. Next, call the method by calling BeginInvoke on the delegate, and wait for the result to return from the server.

VB Async calls
View Source

[This sample can be found at D:\quickstarts.asp.net\QuickStartv20\HowTo\Samples\Remoting\async\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]




Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.