IAsyncResult (Interfaz)
Ensamblado: mscorlib (en mscorlib.dll)
Las clases que contienen métodos que pueden funcionar de forma asincrónica implementan la interfaz IAsyncResult. Es el tipo de valor devuelto por los métodos que inician una operación asincrónica, como FileStream.BeginRead, y es el tipo del tercer parámetro de los métodos que concluyen una operación asincrónica, como FileStream.EndRead. Los objetos IAsyncResult también se pasan a los métodos invocados por los delegados de AsyncCallback al finalizar una operación asincrónica.
Un objeto que admite la interfaz IAsyncResult almacena la información de estado de una operación asincrónica y proporciona un objeto de sincronización que permite marcar los subprocesos cuando finaliza la operación.
Para obtener una descripción detallada de cómo utilizar la interfaz IAsyncResult, vea el tema Llamar a métodos sincrónicos de forma asincrónica.
En el siguiente ejemplo, se muestra la forma de utilizar IAsyncResult para obtener el valor devuelto de una operación asincrónica.
// 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.
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
En el siguiente ejemplo, se muestra la forma de esperar a que finalice una operación asincrónica.
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); } }
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.