//声明委托
public delegate string Deleg(string str);
private void btnSync_Click(object sender, RoutedEventArgs e)
{
//实例化委托并赋值
Deleg deleg = new Deleg(ShowMsg);
//实例化回调方法
AsyncCallback callback = Call;
//开始异步
deleg.BeginInvoke("King1", callback, deleg);
}
/// <summary>
/// 委托调用的方法,执行方法体,生成返回值
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public string ShowMsg(string msg)
{
return msg + ";返回";
}
/// <summary>
/// 回调方法体,ShowMsg执行完成会调用此方法。处理返回值
/// </summary>
/// <param name="result"></param>
public void Call(IAsyncResult result)
{
Deleg deleg = (Deleg)result.AsyncState;
//取到返回值
string res = deleg.EndInvoke(result);
MessageBox.Show(res);
}
个人小站