IAsyncResult-Schnittstelle

Stellt den Status eines asynchronen Vorgangs dar.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Interface IAsyncResult
'Usage
Dim instance As IAsyncResult
[ComVisibleAttribute(true)] 
public interface IAsyncResult
[ComVisibleAttribute(true)] 
public interface class IAsyncResult
/** @attribute ComVisibleAttribute(true) */ 
public interface IAsyncResult
ComVisibleAttribute(true) 
public interface IAsyncResult

Hinweise

Die IAsyncResult-Schnittstelle wird von Klassen implementiert, die Methoden enthalten, die asynchron arbeiten können. Dies ist der Rückgabetyp von Methoden, die einen asynchronen Vorgang initiieren, z. B. FileStream.BeginRead, und der Typ des dritten Parameters von Methoden, die einen asynchronen Vorgang abschließen, z. B. FileStream.EndRead. IAsyncResult-Objekte werden auch an Methoden übergeben, die beim Abschluss eines asynchronen Vorgangs von AsyncCallback-Delegaten aufgerufen werden.

In einem Objekt, das die IAsyncResult-Schnittstelle unterstützt, werden Zustandsinformationen bezüglich eines asynchronen Vorgangs gespeichert. Es stellt ein Synchronisierungsobjekt bereit, mit dem Threads die Beendigung eines Vorgangs signalisiert werden kann.

Eine ausführliche Beschreibung der Verwendung der IAsyncResult-Schnittstelle finden Sie unter dem Thema Asynchrones Aufrufen von synchronen Methoden.

Beispiel

Das folgende Beispiel veranschaulicht das Abrufen des Rückgabewerts eines asynchronen Vorgangs mithilfe von IAsyncResult.

' 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(ar)
   
   Console.Write("Simple.SomeMethod (AsyncCallback): Result of ")
   Console.WriteLine("{0} in SampleSynchronized.Square is {1} ", value, result)
End Sub 'MyCallback  
// 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);

    Console.Write("Simple.SomeMethod (AsyncCallback): Result of ");
    Console.WriteLine("{0} in SampleSynchronized.Square is {1} ", value, result);
}
// Asynchronous Callback method.
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 = dynamic_cast<AsyncResult^>(ar);
   SampSyncSqrDelegate^ temp = static_cast<SampSyncSqrDelegate^>(aResult->AsyncDelegate);
   int result = temp->EndInvoke( ar );
   Console::Write( "Simple::SomeMethod (AsyncCallback): Result of " );
   Console::WriteLine( " {0} in SampleSynchronized::Square is {1} ", value, result );
}
// Asynchronous Callback method.
public static void MyCallback(IAsyncResult ar)
{
    // Obtains the last parameter of the delegate call.
    SampSyncSqrDelegate sampDelg = (SampSyncSqrDelegate)ar.get_AsyncState();
    
    // Obtains return value from the delegate call using EndInvoke.
    AsyncResult aResult = (AsyncResult)ar;
    SampSyncSqrDelegate temp =
        (SampSyncSqrDelegate)(aResult.get_AsyncDelegate());
    int threadId = AppDomain.GetCurrentThreadId();
    int result = temp.EndInvoke(ar);

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

Das folgende Beispiel veranschaulicht das Warten auf den Abschluss eines asynchronen Vorgangs.

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
      Console.Write("SampleSyncronized.Square called.  ")
      Console.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
      Console.Write("Making a synchronous call on the object.  ")
      Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode())
      callParameter = 10
      callResult = sampSyncObj.Square(callParameter)
      Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult)
      Console.WriteLine("")
      Console.WriteLine("")
      
      
      'call the method asynchronously
      Console.Write("Making an asynchronous call on the object.  ")
      Console.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(aResult)

      Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult)

   End Sub 'Main

End Class 'AsyncResultSample
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)
    {
        Console.Write("SampleSyncronized.Square called.  ");
        Console.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
        Console.Write("Making a synchronous call on the object.  ");
        Console.WriteLine("The hash of the current thread is: {0}", Thread.CurrentThread.GetHashCode());
        callParameter = 10;
        callResult = sampSyncObj.Square(callParameter);
        Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.\n\n", callParameter, callResult);


        //call the method asynchronously
        Console.Write("Making an asynchronous call on the object.  ");
        Console.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);
        Console.WriteLine("Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult);
    }
}
using namespace System;
using namespace System::Threading;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Contexts;
using namespace System::Runtime::Remoting::Messaging;

//
// Context-Bound type with Synchronization Context Attribute
//
public ref class SampleSyncronized: public ContextBoundObject
{
public:

   // A method that does some work - returns the square of the given number
   int Square( int i )
   {
      Console::Write( "SampleSyncronized::Square called.  " );
      Console::WriteLine( "The hash of the current thread is: {0}", Thread::CurrentThread->GetHashCode() );
      return i * i;
   }

};

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

//Main sample class
int 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 = gcnew SampleSyncronized;
   
   //call the method synchronously
   Console::Write( "Making a synchronous call on the Object*.  " );
   Console::WriteLine( "The hash of the current thread is: {0}", Thread::CurrentThread->GetHashCode() );
   callParameter = 10;
   callResult = sampSyncObj->Square( callParameter );
   Console::WriteLine( "Result of calling sampSyncObj.Square with {0} is {1}.\n\n", callParameter, callResult );
   
   //call the method asynchronously
   Console::Write( "Making an asynchronous call on the Object*.  " );
   Console::WriteLine( "The hash of the current thread is: {0}", Thread::CurrentThread->GetHashCode() );
   SampSyncSqrDelegate^ sampleDelegate = gcnew SampSyncSqrDelegate( sampSyncObj, &SampleSyncronized::Square );
   callParameter = 17;
   IAsyncResult^ aResult = sampleDelegate->BeginInvoke( callParameter, nullptr, 0 );
   
   //Wait for the call to complete
   aResult->AsyncWaitHandle->WaitOne();
   callResult = sampleDelegate->EndInvoke( aResult );
   Console::WriteLine( "Result of calling sampSyncObj.Square with {0} is {1}.", callParameter, callResult );
}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

IAsyncResult-Member
System-Namespace