C# 子线程怎么操作主线程的组件??比如说主线程有个 textBox,子线程要往textBox里面追加一个字符串,怎么实现?

解决方案 »

  1.   


    public void StartWithArg()
            {
                structB stNum = new structB();
                stNum.structNum = new structA[100];
                Thread thread1 = new Thread(delegate() { Run(txtName); });
                thread1.Start();
            }        public void Run(TextBox txt)
            {
                txt.Text += "run";
            }
      

  2.   

    需要通过 Control.Invoke或Control.BeginInvoke方法来进行处理
    Control.CheckForIllegalCrossThreadCalls=false;delegate void AddDelegate();
            event AddDelegate addEvent;        private void Form1_Load(object sender, EventArgs e)
            {
                addEvent+= new AddDelegate(TEST);
                Thread myThread = new Thread(new ThreadStart(this.Add));
                myThread.Start();
            }
            private void TEST()
            {
                listBox1.Items.Add("a");
            }
            private void Add()
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(addEvent);
                }
                else
                {
                    listBox1.Items.Add("ip");                
                }
            }
      

  3.   

    顶  
    Control.Invoke或Control.BeginInvoke方法
      

  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 ThreadLableTest
    {
        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.btnStart = new System.Windows.Forms.Button();
                this.label1 = new System.Windows.Forms.Label();
                this.btnStop = new System.Windows.Forms.Button();
                this.SuspendLayout();
                this.btnStart.Location = new System.Drawing.Point(176, 107);
                this.btnStart.Name = "btnStart";
                this.btnStart.Size = new System.Drawing.Size(75, 23);
                this.btnStart.TabIndex = 0;
                this.btnStart.Text = "开始移动";
                this.btnStart.UseVisualStyleBackColor = true;
                this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(12, 39);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(59, 12);
                this.label1.TabIndex = 1;
                this.label1.Text = "移动Label";
                this.btnStop.Location = new System.Drawing.Point(257, 107);
                this.btnStop.Name = "btnStop";
                this.btnStop.Size = new System.Drawing.Size(75, 23);
                this.btnStop.TabIndex = 2;
                this.btnStop.Text = "停止移动";
                this.btnStop.UseVisualStyleBackColor = true;
                this.btnStop.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(345, 208);
                this.Controls.Add(this.btnStop);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.btnStart);
                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.Button btnStart;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Button btnStop;
            #endregion
            private volatile bool _shouldStop;
            public Form1()
            {
                InitializeComponent();
                _shouldStop = false;        }
            private void btnStart_Click(object sender, EventArgs e)
            {
                _shouldStop = false;
                Thread move = new Thread(new ThreadStart(MoveLabel));
                move.Start();
            }
            private delegate void MoveHandler(Point point);
            private void DoMove(Point point)
            {
                this.label1.Location = point;
            }
            private void MoveLabel()
            {
                while (!_shouldStop)
                {
                    Point nowPoint = this.label1.Location;
                    Point newPoint = new Point(nowPoint.X + 1, nowPoint.Y);
                    this.label1.Invoke(new MoveHandler(DoMove), new object[] { newPoint });
                    Thread.Sleep(100);
                }
            }
            private void btnStop_Click(object sender, EventArgs e)
            {
                _shouldStop = true;
            }
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                _shouldStop = true;
                Environment.Exit(0);
            }
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
      

  5.   

    使用委托。委托可以传递class的指针
      

  6.   

    winform就需要 一部调用 代理。 Wpf 这有this.Dispatcher.Invoke