public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        ThreadStat thst = new ThreadStat(new Accept().add);
        Thread thread = null;
        private ListView listView1;        public ListView MyListView
        {
            get
            {
                return this.listView1;
            }
            set
            {
                this.listView1 = value;
            }
        }        private void Form1_Load(object sender, EventArgs e)
        {            #region  对ListVi的设置            this.listView1.GridLines = true;
            this.listView1.View = View.Details;
            this.listView1.LabelEdit = true;
            this.listView1.Scrollable = true;
            this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
            this.listView1.FullRowSelect = true;
            this.listView1.HotTracking = true;
            this.listView1.HoverSelection = true;            //this.listView1.Activation = ItemActivation.Standard;            //添加表头
            this.listView1.Columns.Add("hello",170);
            this.listView1.Columns.Add("world",170);
            
            #endregion
            thread = new Thread(thst);
            thread.IsBackground = true;
            thread.start();
            // 还有其他线程, 在此没写
        }
     }
又定义了一个类:
Class Accept 
{
    delegate void SetListView(ListViewItem item);
    public void add()
    {
        string str1 = "aaa";   //此变量的内容是变的, 在此为了方便赋了具体值
        string str2 = "bbb";    //同上        ListViewItem item = new ListViewItem(new string[] {str1,str2});
        this.addListViewItem(ite1);    //调用委托
    }
 
    //委托管理
    public void addListViewItem(ListViewItem item)
    {
        Form1 form = new Form1();        if (form.MyListView.InvokeRequired)
        {
             SetListView d = new SetListView(addListViewItem);
             form.MyListView.Invoke(d, new object[] { item });
         }
         else
         {
             form.MyListView.Items.Add(item);
         }        }
}这是我根据自己写的类的情况,暂时写的一个, 可能有拼写上的错误, 大体就是这样。俺在网上查的在多线程中对WinForm的控件操作要用delegete,俺就用了, 但是还是没学明白,导致向ListView中添加Item成功不了, 寻求帮助。最好贴上解决的代码。呵呵,谢谢

解决方案 »

  1.   

    对着你的修改了一下。你仔细看。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;namespace CSharpWin02
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        Thread thread = null;        public ListView MyListView
            {
                get
                {
                    return this.listView1;
                }
                set
                {
                    this.listView1 = value;
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                #region  对ListVi的设置            this.listView1.GridLines = true;
                this.listView1.View = View.Details;
                this.listView1.LabelEdit = true;
                this.listView1.Scrollable = true;
                this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
                this.listView1.FullRowSelect = true;
                this.listView1.HotTracking = true;
                this.listView1.HoverSelection = true;            //this.listView1.Activation = ItemActivation.Standard;            //添加表头
                this.listView1.Columns.Add("hello", 170);
                this.listView1.Columns.Add("world", 170);            #endregion
                thread = new Thread(new Accept(this.listView1).add);
                thread.IsBackground = true;
                thread.Start();
            }
        }    class Accept
        {
            ListView _host = null;
            public Accept(ListView host)
            {
                _host = host;
            }
            delegate void SetListView(ListViewItem item);
            public void add()
            {
                string str1 = "aaa";   //此变量的内容是变的, 在此为了方便赋了具体值
                string str2 = "bbb";    //同上            ListViewItem item = new ListViewItem(new string[] { str1, str2 });
                if (_host.InvokeRequired)//需要同步
                {
                    _host.Invoke((EventHandler)delegate
                    {
                        _host.Items.Add(item);
                    });
                }
                else//直接操作
                {
                    _host.Items.Add(item);
                }
            }
        }
    }
      

  2.   

    写得好乱,你先看一下委托与线程
    http://www.cnblogs.com/zhangpengshou/archive/2008/04/28/1175289.html
      

  3.   


            public Form1()
            {
                InitializeComponent();//加上这个
                Form1.CheckForIllegalCrossThreadCalls = false;
            }  
      

  4.   

    代码如下:测试OK
    delegate void AddListView(ListViewItem lvi);        private void AddLST(ListViewItem lvi)
            {
                if (listViewEx1.InvokeRequired)
                {
                    AddListView lst = new AddListView(AddLST);
                    this.Invoke(lst, lvi);
                    return;
                }
                listViewEx1.Items.Add(lvi);
            }        private void ListViewAdd()
            {
                string str1 = "aaa";   //此变量的内容是变的, 在此为了方便赋了具体值
                string str2 = "bbb";    //同上            ListViewItem item = new ListViewItem(new string[] { str1, str2 });
                AddLST(item);
            }private void button1_Click(object sender, EventArgs e)
            {
                Thread th = new Thread(new ThreadStart(ListViewAdd));
                th.Start();
                //Form2 f2 = new Form2();
                //f2.Show();
            }
      

  5.   

    额 你测试的可以, 但我把代码写到我程序里面却不行啊。
    如果说我的ListView在一个TableControl的容器里面, 是不是会对添加Item事件产生影响?难道是这个地方的差异?
      

  6.   


    俺是新手, 你写的代码 俺看不懂, 能否解释一下, 还有俺的ListView在一个TabControl里面,是不是跟直接在Form窗体中又不一样? 我的QQ: 489272756 
      

  7.   

    是的,由于你使用了TABCONTROL
    使用多线程同步多控件的话,就没多大的意义,反而使用单线程比较好
      

  8.   

    楼主如何解决的,能说一下么?
    我的是 Add 上去之后没有显示,也不知道怎么回事。郁闷是,忘楼主指点下