c#怎样利用this.Invoke()方法,用委托调用带参数的方法?new ParameterizedThreadStart()除外 。
兄弟帮帮忙。

解决方案 »

  1.   

    Invoke一般是用在跨线程调用的时候使用using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Threading;   public class MyFormControl : Form
       {
          public delegate void AddListItem(String myString);
          public AddListItem myDelegate;
          private Button myButton;
          private Thread myThread;
          private ListBox myListBox;
          public MyFormControl()
          {
             myButton = new Button();
             myListBox = new ListBox();
             myButton.Location = new Point(72, 160);
             myButton.Size = new Size(152, 32);
             myButton.TabIndex = 1;
             myButton.Text = "Add items in list box";
             myButton.Click += new EventHandler(Button_Click);
             myListBox.Location = new Point(48, 32);
             myListBox.Name = "myListBox";
             myListBox.Size = new Size(200, 95);
             myListBox.TabIndex = 2;
             ClientSize = new Size(292, 273);
             Controls.AddRange(new Control[] {myListBox,myButton});
             Text = " 'Control_Invoke' example ";
             myDelegate = new AddListItem(AddListItemMethod);
          }
          static void Main()
          {
             MyFormControl myForm = new MyFormControl();
             myForm.ShowDialog();
          }
          public void AddListItemMethod(String myString)
          {
                myListBox.Items.Add(myString);
          }
          private void Button_Click(object sender, EventArgs e)
          {
             myThread = new Thread(new ThreadStart(ThreadFunction));
             myThread.Start();
          }
          private void ThreadFunction()
          {
             MyThreadClass myThreadClassObject  = new MyThreadClass(this);
             myThreadClassObject.Run();
          }
       }
       public class MyThreadClass
       {
          MyFormControl myFormControl1;
          public MyThreadClass(MyFormControl myForm)
          {
             myFormControl1 = myForm;
          }
          String myString;      public void Run()
          {
             for (int i = 1; i <= 5; i++)
             {
                myString = "Step number " + i.ToString() + " executed";
                Thread.Sleep(400);
                // Execute the specified delegate on the thread that owns
                // 'myFormControl1' control's underlying window handle with
                // the specified list of arguments.
                myFormControl1.Invoke(myFormControl1.myDelegate,
                                       new Object[] {myString});
             }
          }
       }
      

  2.   

    Invoke(new A(B), new Object[] { ""});  
    http://topic.csdn.net/u/20100704/12/964493ac-547f-4b54-addb-bf014a459bc1.html
      

  3.   

    this.Invoke((EventHandler)delegate
    {
        //随便你调用什么,有没参数都行,委托不用了。直接调用函数即可。当然直接委托也行。
        委托(参数);
    });
      

  4.   

    这个一般是用来保持界面响应速度的,就是使用多线程来防止界面假死。给你个例子using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;namespace PanelSwitch
    {
        public  class Form1 : Form
        {        #region 窗体设计
            private System.ComponentModel.IContainer components = null;
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
            private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.btnStart = new System.Windows.Forms.Button();
                this.btbStop = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(47, 65);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(210, 21);
                this.textBox1.TabIndex = 0;
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(47, 65);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(210, 21);
                this.textBox2.TabIndex = 1;
                this.textBox2.Visible = false;
                // 
                // btnStart
                // 
                this.btnStart.Location = new System.Drawing.Point(100, 133);
                this.btnStart.Name = "btnStart";
                this.btnStart.Size = new System.Drawing.Size(75, 23);
                this.btnStart.TabIndex = 2;
                this.btnStart.Text = "开始";
                this.btnStart.UseVisualStyleBackColor = true;
                this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                // 
                // btbStop
                // 
                this.btbStop.Location = new System.Drawing.Point(182, 133);
                this.btbStop.Name = "btbStop";
                this.btbStop.Size = new System.Drawing.Size(75, 23);
                this.btbStop.TabIndex = 3;
                this.btbStop.Text = "停止";
                this.btbStop.UseVisualStyleBackColor = true;
                this.btbStop.Click += new System.EventHandler(this.btnStop_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(310, 250);
                this.Controls.Add(this.btbStop);
                this.Controls.Add(this.btnStart);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.textBox1);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "Form1";
                this.Text = "动态切换";
                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
                this.ResumeLayout(false);
                this.PerformLayout();        }
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.Button btnStart;
            private System.Windows.Forms.Button btbStop;
            #endregion        #region 逻辑代码
            private volatile bool _shouldStop;
            public Form1()
            {
                InitializeComponent();
                _shouldStop = false;
            }
            private void btnStart_Click(object sender, EventArgs e)
            {
                this.textBox1.Visible = true;
                this.textBox2.Visible = false;
                _shouldStop = false;
                Thread getNum = new Thread(new ThreadStart(GetRandomNum));
                getNum.Start();
            }
            public delegate void SetValue(int num);
            private void SetTxtBoxConten(int num)
            {
                this.textBox1.Text = num.ToString();
            }
            private void GetRandomNum()
            {
                while (!_shouldStop)
                {
                    Random r = new Random();
                    int num = r.Next(1000000, 9999999);
                    this.textBox1.Invoke(new SetValue(SetTxtBoxConten), new object[] { num });
                    Thread.Sleep(100);
                }
            }
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                _shouldStop = true;
                Environment.Exit(0);
            }
            private void btnStop_Click(object sender, EventArgs e)
            {
                _shouldStop = true;
                this.textBox1.Visible = false;
                this.textBox2.Visible = true;
                this.textBox2.Text = string.Format("你选择了{0}",this.textBox1.Text);
            }
            #endregion        [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }