本文介绍了如何在回调中获取传递给异步方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在 CallbackMethodSendRegistration 中传输到 AsyncSendRegistrationMethod 的标签.
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
{
}
}
推荐答案
获取对传递给 sendRegistrationDelegate 的标签的引用的一种方法是让回调为 lambda.在呼叫站点,这看起来像这样:
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);
这篇关于如何在回调中获取传递给异步方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!