Asynchronous call and Asyncstate

Hi!

How to work with the Asyncstate in the target method?

That is, I call a BeginInvoke with a state object ...

MyDelegateObject DO = new MyDelegateObject(MyTargetMethod);

DO.BeginInvoke(MyCallBack,MyStateObject);

Target method works...

MyTargetMethod()

{

// Where is MyStateObject?

// Code to complete work ...

}

And AsyncCallback is called when target method finish...

MyCallBack(IAsyncResult ar)

{

// ar.AsyncState <-- here is my StateObject, but is the equal that when i call BeginInvoke ...

}

But... I want for example, that the result of the target method modify the state object and thus have a result, or when i call to EndInvoke IAsyncResult.Asyncstate have the results of the target method...

I see that i can use outers variables but... then what's the utility of AsyncState? :P

Regards.

[1599 byte] By [overpeer] at [2007-12-24]
# 1

It's purpose is to provide data to your end handler. The general process for getting, updating and returning data from an async method is to store the value(s) as members of the class that is async invoked. Thus you would have created a class that contains the data to get/set and then async invoke a method on the class to work with the data. When the invocation finishes you can use the class to get the results. The state object is generally set to the instance you were using. However if the callback is on the class already then the state object is not that useful.

Michael Taylor - 9/29/06

TaylorMichaelL at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Thanks.

Regards.

overpeer at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...