Possible Duplicate:
How to get the parameters passed to the asynchronous method in the callback
I need convert this lambda to method callback
var sendRegistrationDelegate =
new AsyncSendRegistrationDelegate(AsyncSendRegistrationMethod);
sendRegistrationDelegate.BeginInvoke(registrationToUser, label, ar =>
{
var responceFromServer = sendRegistrationDelegate.EndInvoke(ar);
if (responceFromSe开发者_如何学JAVArver.IsError)
{
SetText(label, registrationToUser.Name + @" " +
responceFromServer.ErrorMessage);
}
else
{
SetText(label, registrationToUser.Name + @" " +
responceFromServer.Data);
}
}, null);
First, do you have a grasp on lambdas and anonymous delegates?
In this snippet:
sendRegistrationDelegate.BeginInvoke(registrationToUser, label, ar =>
// start of method
{
var responceFromServer = sendRegistrationDelegate.EndInvoke(ar);
if (responceFromServer.IsError)
{
SetText(label, registrationToUser.Name + @" " +
responceFromServer.ErrorMessage);
}
else
{
SetText(label, registrationToUser.Name + @" " +
responceFromServer.Data);
}
}
// end of method
, null);
...the opening and closing { }
mark the beginning and ending of a method, like so:
void AsyncCallbackMethod(IAsyncResult ar)
{
// method body
}
Your BeginInvoke method would look like:
sendRegistrationDelegate.BeginInvoke(registrationToUser, label, new AsyncCallback(AsyncCallbackMethod), null);
精彩评论