IAsyncResult Interface
Assembly: mscorlib (in mscorlib.dll)
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.
The following sample demonstrates using an IAsyncResult to obtain the return value of an asynchronous operation.
// 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
The following sample demonstrates waiting for an asynchronous operation to complete.
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 ); }
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.