委托(delegate)的异步调用
private void ButAbout_Click(object sender, EventArgs e)
{
TestInvoke();
}
public void TestInvoke()
{
System.Func<string, string> convertMethod = UppercaseString;
convertMethod.BeginInvoke(name, new AsyncCallback(sendMessageHandleBack), convertMethod);
}
string name = "Dakota";
void sendMessageHandleBack(IAsyncResult ar)
{
System.Func<string, string> dlgt = (System.Func<string, string>)ar.AsyncState;
string ret = dlgt.EndInvoke(ar);
// int ret= dlgt.EndInvoke<string,int>(sendMessageHandleBack);
NewMethod(ret);
}
private delegate void NewMethodHandle(string ret);
private void NewMethod(string ret)
{
this.BeginInvoke(new NewMethodHandle(NewMethod2), new object[] { ret });
}
private void NewMethod2(string ret)
{
this.textBox1.Text = ret;
}
private static string UppercaseString(string inputString)
{
Thread.Sleep(4000);
return inputString;
}
{
TestInvoke();
}
public void TestInvoke()
{
System.Func<string, string> convertMethod = UppercaseString;
convertMethod.BeginInvoke(name, new AsyncCallback(sendMessageHandleBack), convertMethod);
}
string name = "Dakota";
void sendMessageHandleBack(IAsyncResult ar)
{
System.Func<string, string> dlgt = (System.Func<string, string>)ar.AsyncState;
string ret = dlgt.EndInvoke(ar);
// int ret= dlgt.EndInvoke<string,int>(sendMessageHandleBack);
NewMethod(ret);
}
private delegate void NewMethodHandle(string ret);
private void NewMethod(string ret)
{
this.BeginInvoke(new NewMethodHandle(NewMethod2), new object[] { ret });
}
private void NewMethod2(string ret)
{
this.textBox1.Text = ret;
}
private static string UppercaseString(string inputString)
{
Thread.Sleep(4000);
return inputString;
}