How to get the parameters passed to the asynchrono

2020-02-16 03:39发布

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 = (AsyncSendRegistrationDelegate)ar.AsyncState;

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

    }
}

1条回答
Bombasti
2楼-- · 2020-02-16 03:45

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);
查看更多
登录 后发表回答