lijinchang

导航

一个异步执行的小例子

 

我用的visual studio 2008

winform界面初始化类

 partial class BeginInvoke
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(254, 274);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "执行";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(254, 128);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 1;
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(183, 131);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 2;
            this.label1.Text = "输入个数:";
            //
            // BeginInvoke
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(735, 459);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "BeginInvoke";
            this.Text = "BeginInvoke";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
    }

包括的事件处理类

public partial class BeginInvoke : Form
    {
        public delegate int ProcessDel(int a);
        public BeginInvoke()
        {
            InitializeComponent();
        }
        private int Process(int a)
        {
            Thread.Sleep(10000);
            return a * a;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int count = int.Parse(this.textBox1.Text.Trim());
            ProcessDel pd = new ProcessDel(Process);
            Myobject mobject=new Myobject();
            mobject.message="成功";
            mobject.pd=pd;
            pd.BeginInvoke(count, MethodComplete, mobject);
        }
        private void MethodComplete(IAsyncResult asycresult)
        {
            ProcessDel pd = ((Myobject)asycresult.AsyncState).pd as ProcessDel;
            string message = ((Myobject)asycresult.AsyncState).message;
            int result=pd.EndInvoke(asycresult);
            MessageBox.Show("处理"+message+",处理结果:"+result);
        }
    }
    public class Myobject
    {
        public Delegate pd;
        public string message;
    }

下面是界面

上面实现的功能就是模拟一个长时间处理过程Process(int a),输入一个参数,然后点击按钮进行处理,执行异步操作,最后把处理结果返回给用户。此程序主要用到了委托和委托异步执行。理解delege。begininvoke的用法,尤其是最后一个参数和倒数第二个参数,一个是object类型,一个是回调过程名,回调过程顾名思义就是异步执行完毕后执行的过程。在回调过程里,我们把执行后的结果通过object中包含的delegate实例通过endinvoke(asycresult)进行获取,把要提示的“成功”字符串包含在object里头传给回调过程,通过拆箱操作获取。最后把结果和传递的字符串通过messagebox提示给用户。这样既防止了界面假死也完成了程序功能。了解更多异步执行或者线程只是请参见网址http://www.cnblogs.com/nokiaguy/archive/2008/07/18/1246299.html

posted on 2011-10-26 10:00  lijinchang  阅读(217)  评论(0编辑  收藏  举报