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

霸气de小男生 提交于 2019-11-30 09:56:54

问题


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:


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


来源:https://stackoverflow.com/questions/6554380/how-to-get-the-parameters-passed-to-the-asynchronous-method-in-the-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!