问题
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