看的不是太懂....
    public partial class frmcmdasyn_callback : Form
    {
        // You'll need this delegate in order to display text from a thread
        // other than the form's thread. See the HandleCallback
        // procedure for more information.
        // This same delegate matches both the DisplayStatus 
        // and DisplayResults methods.
        private delegate void DisplayInfoDelegate(string Text);        // This flag ensures that the user doesn't attempt
        // to restart the command or close the form while the 
        // asynchronous command is executing.
        private bool isExecuting;        // This example maintains the connection object 
        // externally, so that it's available for closing.
        private SqlConnection connection;        public frmcmdasyn_callback()
        {
            InitializeComponent();
        }        private string GetConnectionString()
        {
            // To avoid storing the connection string in your code,            
            // you can retrieve it from a configuration file.             // If you have not included "Asynchronous Processing=true" in the
            // connection string, the command will not be able
            // to execute asynchronously.
            return "Data Source=(local);Integrated Security=true;" +
            "Initial Catalog=hr; Asynchronous Processing=true";
        }
        private void DisplayStatus(string Text)
        {
            this.label1.Text = Text;
        }        private void DisplayResults(string Text)
        {
            this.label2.Text = Text;
            DisplayStatus("Ready");
        }        private void Form_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            if (isExecuting)
            {
                MessageBox.Show(this, "Can't close the form until " +
                "the pending asynchronous command has completed. Please wait...");
                e.Cancel = true;
            }
            
        }        private void button1_Click(object sender, System.EventArgs e)
        {
            if (isExecuting)
            {
                MessageBox.Show(this, "Already executing. Please wait until " +
                "the current query has completed.");
            }
            else
            {
                SqlCommand command = null;
                try
                {
                    DisplayResults("");
                    DisplayStatus("Connecting...");
                    connection = new SqlConnection(GetConnectionString());
                    //ALTER PROCEDURE [dbo].[sp_sum]
                    //AS
                    //BEGIN
                    //    declare @i int,
                    //    @sum int
                    //    set @i=0
                    //    set @sum=0
                    //    while (@i<1000)
                    //    begin
                    //        set @i=@i+1
                    //        Set @sum=@sum+@i
                    //    end                    //    waitfor delay '0:0:05'
                    //    return @sum
                    //END                    command = new SqlCommand();
                    command.Connection = connection;
                    command.CommandText = "sp_sum";
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter sp = command.Parameters.Add("@returnValue", SqlDbType.Int);
                    sp.Direction = ParameterDirection.ReturnValue;                    connection.Open();                    DisplayStatus("Executing...");
                    isExecuting = true;
                    // Although it's not required that you pass the 
                    // SqlCommand object as the second parameter in the 
                    // BeginExecuteNonQuery call, doing so makes it easier
                    // to call EndExecuteNonQuery in the callback procedure.
                    AsyncCallback callback = new AsyncCallback(HandleCallback);
                    
                    // Once the BeginExecuteNonQuery method is called,
                    // the code continues--and the user can interact with
                    // the form--while the server executes the query.
                    command.BeginExecuteNonQuery(callback, command);
                                    }
                catch (Exception ex)
                {
                    isExecuting = false;
                    DisplayStatus( 
                     string.Format("Ready (last error: {0})", ex.Message));
                    if (connection != null)
                    {
                        connection.Close();
                    }
                }
            }
        }        private void HandleCallback(IAsyncResult result)
        {
            try
            {
                // Retrieve the original command object, passed
                // to this procedure in the AsyncState property
                // of the IAsyncResult parameter.
                SqlCommand command = (SqlCommand)result.AsyncState;
                command.EndExecuteNonQuery(result);                
                string r = command.Parameters["@returnValue"].Value.ToString();
               
                DisplayInfoDelegate del = new DisplayInfoDelegate(DisplayResults);
                
                this.Invoke(del, r);
            }
            catch (Exception ex)
            {
                
                this.Invoke(new DisplayInfoDelegate(DisplayStatus),
                String.Format("Ready(last error: {0}", ex.Message));
            }
            finally
            {
                isExecuting = false;
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }其中
AsyncCallback callback = new AsyncCallback(HandleCallback);
和private void HandleCallback(IAsyncResult result)函数..
解释下...谢谢...

解决方案 »

  1.   

    AsyncCallback是一个带IAsyncResult参数的委托 new AsyncCallback(HandleCallback)使委托对应到HandleCallback方法。给委托传参就在后面写,实际就是给HandleCallback方法传参了 
      

  2.   

    恩,委托,最近也在看。
    委托声明一种类型,它由一组特定的参数合返回类型来封装方法,可封装静态和实例方法。
    例如:
    有个静态方法
    static void Eat(string food)
    {
    console.WriteLine("张三吃"+food)
    }
    定义一个委托,这个委托返回值类型也是void,而且参数也是string。
    delegate void EatDelegate(string food)
    使用时:
    static void Main()
    {
    EatDelegate eat=new EatDelegate(Eat);
    eat("西瓜");//打印张三吃西瓜
    }
    当然,这只是简单的应用