This documentation is archived and is not being maintained.

AsyncResult.AsyncState Property

Gets the object provided as the last parameter of a BeginInvoke method call.

[Visual Basic]
Public Overridable ReadOnly Property AsyncState As Object  _
   Implements IAsyncResult.AsyncState
[C#]
public virtual object AsyncState {get;}
[C++]
public: __property virtual Object* get_AsyncState();
[JScript]
public function get AsyncState() : Object;

Property Value

The object provided as the last parameter of a BeginInvoke method call.

Implements

IAsyncResult.AsyncState

Remarks

For more information about the BeginInvoke method, see Asynchronous Delegates.

Example

[Visual Basic, C#, C++] The following code example demonstrates how the AsyncState property is used to get the last parameter of a BeginInvoke method call. For the complete example code, see the example for the AsyncResult class.

[Visual Basic] 
' 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  

[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);

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

[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 = dynamic_cast<AsyncResult*>(ar);
    SampSyncSqrDelegate* temp = static_cast<SampSyncSqrDelegate*>(aResult->AsyncDelegate);
    int result = temp->EndInvoke(ar);

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

AsyncResult Class | AsyncResult Members | System.Runtime.Remoting.Messaging Namespace | Asynchronous Delegates

Show: