委托(delegate)的异步调用

 private void ButAbout_Click(object sender, EventArgs e)
        
{
            TestInvoke();
        }


        
public void TestInvoke()
        
{

            System.Func
<stringstring> convertMethod = UppercaseString;
          
           
            convertMethod.BeginInvoke(name, 
new AsyncCallback(sendMessageHandleBack), convertMethod);

         
        }

        
string name = "Dakota";
        
void sendMessageHandleBack(IAsyncResult ar)
        
{
            System.Func
<stringstring> dlgt = (System.Func<stringstring>)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;
        }
posted @ 2007-12-21 15:25  瑞德船长  阅读(557)  评论(0编辑  收藏  举报