开发者

How to get the parameters passed to the asynchronous method in the callback (not lambda) [duplicate]

This question already has an answer here: Closed 11 years ago.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜