asp.net.ph

IAsyncResult Interface

System Namespace


Represents the status of an asynchronous operation.

IAsyncResult Interface Members

Collapse   Properties

Visibility Name Value Type Accessibility
public AsyncState Object [ Get ]
public AsyncWaitHandle WaitHandle [ Get ]
public CompletedSynchronously Boolean [ Get ]
public IsCompleted Boolean [ Get ]

Classes that Implement IAsyncResult


Class Description
AsyncResult Encapsulates the results of an asynchronous operation on an asynchronous delegate.
WebClientAsyncResult Provides an implementation of IAsyncResult for use by XML Web service proxies to implement the standard asynchronous method pattern.

Remarks

The IAsyncResult interface is implemented by classes containing methods that can operate asynchronously. It is the return type of methods that initiate an asynchronous operation, such as FileStream.BeginRead, and is the type of the third parameter of methods that conclude an asynchronous operation, such as FileStream.EndRead. IAsyncResult objects are also passed to methods invoked by AsyncCallback delegates when an asynchronous operation completes.

An object that supports the IAsyncResult interface stores state information for an asynchronous operation, and provides a synchronization object to allow threads to be signaled when the operation completes.

For a detailed description of how the IAsyncResult interface is used, see the Asynchronous Programming Overview topic.

Example

The following sample demonstrates using an IAsyncResult to obtain the return value of an asynchronous operation.

[ VB ]
' Asynchronous Callback method.
Public Shared Sub MyCallback ( ar As IAsyncResult )
   ' Obtains the last parameter of the delegate call.
   Dim value As Integer = Convert.ToInt32 ( ar.AsyncState )
  
   ' Obtains return value from the delegate call using EndInvoke.
   Dim aResult As AsyncResult = CType ( ar, AsyncResult )
   Dim temp As SampSyncSqrDelegate = CType ( aResult.AsyncDelegate, SampSyncSqrDelegate )
   Dim result As Integer = temp.EndInvoke ( 0, ar )
  
   Response.Write ( "Simple.SomeMethod ( AsyncCallback ) : Result of " )
   Response.WriteLine ( "{0} in SampleSynchronized.Square is {1} ", value, result )
End Sub 'MyCallback 

[ C# ]
// Asynchronous Callback method.
public static void MyCallback ( IAsyncResult ar ) {

    // Obtains the last parameter of the delegate call.
    int value = Convert.ToInt32 ( ar.AsyncState );

    // Obtains return value from the delegate call using EndInvoke.
    AsyncResult aResult = ( AsyncResult ) ar;
    SampSyncSqrDelegate temp = ( SampSyncSqrDelegate ) aResult.AsyncDelegate;
    int result = temp.EndInvoke ( ar );

    Response.Write ( "Simple.SomeMethod ( AsyncCallback ) : Result of " );
    Response.WriteLine ( "{0} in SampleSynchronized.Square is {1} ", value, result );
}

The following sample demonstrates waiting for an asynchronous operation to complete.

[ VB ]
Imports System
Imports System.Threading
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Contexts
Imports System.Runtime.Remoting.Messaging


'
' Context-Bound type with Synchronization Context Attribute
'
<Synchronization ( ) > Public Class SampleSyncronized
   Inherits ContextBoundObject
  
   ' A method that does some work - returns the square of the given number
   Public Function Square ( i As Integer ) As Integer
      Response.Write ( "SampleSyncronized.Square called.  " )
      Response.WriteLine ( "The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode ( ) )
      Return i * i
   End Function 'Square
End Class 'SampleSyncronized



'
' Async delegate used to call a method with this signature asynchronously
'
Delegate Function SampSyncSqrDelegate ( i As Integer ) As Integer


'Main sample class
Public Class AsyncResultSample
  
   Public Shared Sub Main ( )
      Dim callParameter As Integer = 0
      Dim callResult As Integer = 0
     
      'Create an instance of a context-bound type SampleSynchronized
      'Because SampleSynchronized is context-bound, the object sampSyncObj
      'is a transparent proxy
      Dim sampSyncObj As New SampleSyncronized ( )
     
     
      'call the method synchronously
      Response.Write ( "Making a synchronous call on the object.  " )
      Response.WriteLine ( "The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode ( ) )
      callParameter = 10
      callResult = sampSyncObj.Square ( callParameter )
      Response.WriteLine ( "Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult )
      Response.WriteLine ( "" )
      Response.WriteLine ( "" )
     
     
      'call the method asynchronously
      Response.Write ( "Making an asynchronous call on the object.  " )
      Response.WriteLine ( "The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode ( ) )
      Dim sampleDelegate As New SampSyncSqrDelegate ( AddressOf sampSyncObj.Square )
      callParameter = 17
     
      Dim aResult As IAsyncResult = sampleDelegate.BeginInvoke ( callParameter, Nothing, Nothing )
     
      'Wait for the call to complete
      aResult.AsyncWaitHandle.WaitOne ( )
     
      callResult = sampleDelegate.EndInvoke ( callResult, aResult )
      Response.WriteLine ( "Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult )
   End Sub 'Main
End Class 'AsyncResultSample

[ C# ]
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;

//
// Context-Bound type with Synchronization Context Attribute
//
[ Synchronization ( ) ]
public class SampleSyncronized : ContextBoundObject
{
    // A method that does some work - returns the square of the given number
    public int Square ( int i )
    {
        Response.Write ( "SampleSyncronized.Square called.  " );
        Response.WriteLine ( "The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode ( ) );
        return i*i;
    }
}


//
// Async delegate used to call a method with this signature asynchronously
//
public delegate int SampSyncSqrDelegate ( int i );

//Main sample class
public class AsyncResultSample
{
    public static void Main ( )
    {
        int callParameter = 0;
        int callResult = 0;

        //Create an instance of a context-bound type SampleSynchronized
        //Because SampleSynchronized is context-bound, the object sampSyncObj
        //is a transparent proxy
        SampleSyncronized sampSyncObj = new SampleSyncronized ( );


        //call the method synchronously
        Response.Write ( "Making a synchronous call on the object.  " );
        Response.WriteLine ( "The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode ( ) );
        callParameter = 10;
        callResult = sampSyncObj.Square ( callParameter );
        Response.WriteLine ( "Result of calling sampSyncObj.Square with {0} is {1}.\n\n", callParameter, callResult );


        //call the method asynchronously
        Response.Write ( "Making an asynchronous call on the object.  " );
        Response.WriteLine ( "The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode ( ) );
        SampSyncSqrDelegate sampleDelegate = new SampSyncSqrDelegate ( sampSyncObj.Square );
        callParameter = 17;

        IAsyncResult aResult = sampleDelegate.BeginInvoke ( callParameter, null, null );

        //Wait for the call to complete
        aResult.AsyncWaitHandle.WaitOne ( );

        callResult = sampleDelegate.EndInvoke ( aResult );
        Response.WriteLine ( "Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult );
    }
}
Skip Navigation Links



Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph