开发者

How to get the parameters passed to the asynchronous method in the callback

开发者 https://www.devze.com 2023-03-16 17:02 出处:网络
I need a Label that is transmitted to the AsyncSendRegistrationMethod in CallbackMethodSendRegistration.

I need a Label that is transmitted to the AsyncSendRegistrationMethod in CallbackMethodSendRegistration.

private delegate ResponceFromServer AsyncSendRegistrationDelegate(RegistrationToUser registrationToUser, Label label);
private ResponceFromServer AsyncSendRegistrationMethod(RegistrationToUser registrationToUser, Label label)
{
    SetText(label,  registrationToUser.Name + " registration...");


    return Requests.DataBase.Authorization.Registration(
        registrationToUser.Name, 
        registrationToUser.IdRoleUser, 
        registrationToUser.IdGroup);
}
private void CallbackMethodSendRegistration(IAsyncResult ar)
{
    var sendRegistrationDelegate开发者_JAVA技巧 = (AsyncSendRegistrationDelegate)ar.AsyncState;

    var responceFromServer = (ResponceFromServer)sendRegistrationDelegate.EndInvoke(ar);
    if(responceFromServer.IsError)
    {
       //here need label.Text
    }
    else
    {

    }
}


One way to get a reference to the label passed to sendRegistrationDelegate is by having the callback be a lambda. At the call site this would look like this:

    var registrationToUser = ...;
    var label = ...;

    sendRegistrationDelegate.BeginInvoke(registrationToUser, label, ar =>
    {
        var responceFromServer  = sendRegistrationDelegate.EndInvoke(ar);
        if (responceFromServer.IsError)
        {
            label.Text = "";
        }
        else
        {

        }
    }, null);
0

精彩评论

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