C#为什么多线程控制winform需要用委托?比如我新起了一个线程A,在A线程里要对winform的list控件里显示数据,我需要用一个委托函数来实现。
 
因为winform是主线程创建的,你用另外一个线程来调用它就可能会出现两个线程同时访问同一个资源的问题,这个时候很容易出现错误,比如A线程改变窗口的颜色为红色,B线程取窗口的颜色,如果这两个线程正好碰到一起,可能A略先于B,那么就是B取出来的红色,如果略后于那么就是原来的颜色,而这和CPU的繁忙度、时间片的轮转是相关的,是一种随机的情况,那么B取出来的颜色就不可靠了,因此为了避免这种状况采用委托,B线程向A线程发出委托,由A线程来完成取色工作,那么可以保证取色工作的稳定性,结果也可靠

如果你实在想通过另一个线程访问winform,可以在窗口的构造函数中加入
Control.CheckForIllegalCrossThreadCalls = false;
这样可以屏蔽这个错误,不过建议还是用委托
 
这需要用到委托.
你先声明一个委托,然后,把一个操作界面的函数托给他.
你的线程里只要调用这个委托就可以了.
 
C#试写一个多线程问题(委托,Invoke(),beginInvoke())
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.ComponentModel;
InBlock.gif using System.Data;
InBlock.gif using System.Drawing;
InBlock.gif using System.Text;
InBlock.gif using System.Windows.Forms;
InBlock.gif using System.Threading;
InBlock.gif using System.Diagnostics;
InBlock.gif
InBlock.gif namespace _
InBlock.gif{
InBlock.gif         public partial class Form1 : Form
InBlock.gif        {
InBlock.gif                 public delegate void del( string str);
InBlock.gif                 public delegate void del1( string str1);
InBlock.gif                Thread thread1,thread2;
InBlock.gif                 public Form1()
InBlock.gif                {
InBlock.gif                        InitializeComponent();
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void button1_Click( object sender, EventArgs e)
InBlock.gif                {
InBlock.gif                         this.thread1 = new Thread( new ThreadStart( this.sa));
InBlock.gif                         this.thread1.Start();
InBlock.gif                         //MessageBox.Show(this,);
InBlock.gif                         this.button1.Enabled = false;
InBlock.gif                         //获取当前的线程好
InBlock.gif                         int thread1id = Thread.CurrentThread.GetHashCode();
InBlock.gif                         string strThreadId = Convert.ToString(thread1id);
InBlock.gif                        MessageBox.Show( this,strThreadId);
InBlock.gif                }                
InBlock.gif
InBlock.gif                 private void progressSet( string text)
InBlock.gif                {
InBlock.gif                                progressBar1.Value = 0;
InBlock.gif                                progressBar1.Maximum = 999999;
InBlock.gif                                 for ( int i = 0; i < 999999; i++)
InBlock.gif                                {
InBlock.gif                                        progressBar1.Value++;
InBlock.gif                                }
InBlock.gif                                progressBar1.Value = 0;                        
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void sa()
InBlock.gif                {
InBlock.gif                        del pro = new del(progressSet);
InBlock.gif                         this.Invoke(pro, new object[] { "hello" });
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void button2_Click( object sender, EventArgs e)
InBlock.gif                {
InBlock.gif                         this.thread2 = new Thread( this.sa1);
InBlock.gif                        thread2.Start();
InBlock.gif                         this.button2.Enabled = false;
InBlock.gif                         //获取当前的线程好
InBlock.gif                         int thread2id = Thread.CurrentThread.GetHashCode();
InBlock.gif                         string strThreadId = Convert.ToString(thread2id);
InBlock.gif                        MessageBox.Show( this, strThreadId);
InBlock.gif                }
InBlock.gif
InBlock.gif                 private void sa1()
InBlock.gif                {
InBlock.gif                         //其他操作    
InBlock.gif                         //比如将界面的TextBox内容设置一下    
InBlock.gif                        del1 mi1 = new del1(UpdateTextBox);
InBlock.gif                         this.Invoke(mi1, new object[] { "我是一个文本框" });
InBlock.gif                }
InBlock.gif
InBlock.gif                 //更新界面的方法    
InBlock.gif                 private void UpdateTextBox( string str)
InBlock.gif                {
InBlock.gif                         //更新    
InBlock.gif                         this.textBox1.Text = str;
InBlock.gif                }
InBlock.gif        }
InBlock.gif}