public void run()
        {
            startServer = new Thread(new ThreadStart(startUdpServer));
           
            startServer.Start();
            Thread.Sleep(1000);
            
        }    private void button7_Click(object sender, EventArgs e)
        {
            run();
        }
startUdpServer函数内容如下:
                int i, j;
                byte[] receive = new byte[1200];
                byte[] realdata = new byte[256];
                ASCIIEncoding s = new ASCIIEncoding();                udp.Connect("192.168.7.69", 1969);
                IPEndPoint ipadr = new IPEndPoint(IPAddress.Parse("192.168.7.69"), 10001);
                receive = udp.Receive(ref ipadr);
                if (receive.Length > 119)
                {
                    for (i = 0; i < (int)(receive.Length / 120); i++)
                    {
                        st = "";
                        for (j = 0; j < 29; j++)
                        {
                            Array.Resize(ref realdata, 29);
                            realdata[j] = receive[i * 120 + 0x4d + j];
                        }
                        st += s.GetString(realdata);                    }
                    ListViewItem item;
                    string str = textBox1.Text;
                    ddd = 1;
                    if (listView1.Items.Count < 1)// 如果 listview上的记录为空 则添加一个记录;
                    {
                        item = new ListViewItem(ddd.ToString());
                        item.SubItems.Add(st);
                        item.SubItems.Add("1");
                        listView1.Items.Add(item);
                        int aaa = listView1.Items.Count;
                    }
                    else             
                    {
                        for (int m = 0; m < listView1.Items.Count; m++)   
                        {
                            if (st == Convert.ToString(listView1.Items[m].SubItems[1].Text))
                            {
                             
                                //如果 新的内容 str,和 listview上的记录行 相同 则做
                                ttt = m;
                                kkk = kkk + 1;
                            }
                            else   //如果  listview 上 tag  列的内容和 str 不相等
                            {
                                kkk = 0;
                            }
                        }                        if (kkk == 0)      //如果没有相同的记录则做如下操作,添加新的一条记录;
                        {
                            ddd = ddd + 1;
                            item = new ListViewItem(ddd.ToString());
                            item.SubItems.Add(st);
                            item.SubItems.Add("1");
                            listView1.Items.Add(item);
                        }
                        else    //若记录相等 则给listview 上的 times 列 的加“1”;
                        {
                            listView1.Items[ttt].SubItems[2].Text = Convert.ToString((Convert.ToInt32(listView1.Items[ttt].SubItems[2].Text) + 1));
                        }
                    }
                }            }

解决方案 »

  1.   

    问题出现在startUdpServer函数 因为listview 这个控件吧Cross-thread operation not valid: control'listview' accessed from a thead
    other than thread it was create on.该如何处理?  用delegate么要?那位好心 帮下 谢谢
      

  2.   

    GOOD GOOD STUDY, DAY DAY UP
      

  3.   

    用listView1.Invoke()方法...
    详细的自己去看MSDN...
      

  4.   

    如果要在线程中为控件赋值请在构造函数中加上CheckForIllegalCrossThreadCalls = false;
      

  5.   

    如果要在线程中为控件赋值请在构造函数中加上CheckForIllegalCrossThreadCalls = false;加哪里?
      

  6.   

    是RUN 里
    还是 StartUdpServer 里
      

  7.   

    CheckForIllegalCrossThreadCalls = false;哥们 加了这句 是可以显示了 但只显示了一次数据啊应该不断刷新 listview 的啊  可以看到数据不断变化的但只有一次
      

  8.   

    你用的是Vs2005吧,因为2005处于安全的考虑,不容许线程间相互调用。
    两种方法可以解决:
    1.构造函数里加CheckForIllegalCrossThreadCalls = false;此时就和2003是一样的了,但可能会有一些问题,就像你遇到的,界面刷新可能有点问题,建议使用下面一种解决方法.
    2.用BeginInvoke+delegate,我给你摘点代码,楼主自己研究一下,对你来说应该不难.
      

  9.   

    回调函数入口:
    private void ThreadFun()        {
                //Create invoke method by specific function            MethodInvoker mi = new MethodInvoker( this.InvokeFun );
                for( int i = 0; i < 100; i++ )
                {
                    this.BeginInvoke( mi );//用于界面更新
                    Thread.Sleep( 100 );
                }
            }
    Invoke函数入口:
    private void InvokeFun()        {
                if( prgBar.Value < 100 )
                    prgBar.Value = prgBar.Value + 1;
            }
      

  10.   

    public delegate void MyInvoke(ListViewItem item); //定义一个委托//在要对listview进行添加的地方写上如下代码:
    MyInvoke mi = new MyInvoke(this.MyInvokeMethold);
                this.BeginInvoke(mi, new object[] { item});//更新界面程序段入口
    private void MyInvokeMethold(ListViewItem item)
            {
                //进行添加操作
                  ....
            }没有测试,楼主不妨一试。
      

  11.   

    恩 我先试一下 我对listview 要添加四个列数据   
     this.BeginInvoke(mi, new object[] { item});  object 后就一 参数啊??
      

  12.   

    恩 谢谢啊那位能帮帮改改啊 让数据显示在listview上其实现在做实验测试功能 想显示数据看看, 
    而最终 是以DLL文件 加入 BizTalk2006中的以前没搞过 接触C#也不长 见笑了
      

  13.   

    你用的 .net 2.0 吧,因为现在线程管理比较严格了看我的方法,在多线程的程序中这样写:
    delegate void SetText2CallBack(string text, object sender, int type);
    private void SetText2(string text, object sender, int type)
            {
                TextBox tb = sender as TextBox;
                if (tb == null)
                    return;
                if (tb.InvokeRequired)
                {
                    SetText2CallBack d = new SetText2CallBack(SetText2);
                    this.Invoke(d, new object[] { text, sender, type });
                }
                else
                {
                    if (type == 1)
                    {
                        tb.Text = text;
                    }
                }
            }在子线程中要向某个控件(例如文本框)写数据的时候调用:
    注 text:要写的字符串   sender:要写的控件   type:写得方式,如覆盖或追加等 
    SetText2(string text, object sender, int type)
      

  14.   

    可以参考一下 handsomebird123(handsomebird123) 的代码。
      

  15.   

    有点疑问
    item.SubItems.Add(st);
    item.SubItems.Add("1");
    不就两列吗?你的代码中怎么会出现"subItems[2]"呢?
    是我理解有误,还是你写的有误?
      

  16.   

    有点疑问
    item.SubItems.Add(st);
    item.SubItems.Add("1");
    不就两列吗?你的代码中怎么会出现"subItems[2]"呢?
    是我理解有误,还是你写的有误?
    没写错啊   其实显示的有三列内容  ID  TAG  TIMES"subItems[2]" 是次数啊,  意思是: 如果读到卡和每一行的卡号比较
     比较后确定相同 "subItems[2]" 上就加"1" 次不同 就另起一行的
      

  17.   

    item = new ListViewItem(ddd.ToString());这个也是一列
    下面是 初始代码   int ttt;
            int kkk;
            int ddd;
     listView1.View = View.Details;
                // Add a column with width 20 and left alignment.
                listView1.Columns.Add("ID", 30, HorizontalAlignment.Left);
                listView1.Columns.Add("Tag", 120, HorizontalAlignment.Left);
                listView1.Columns.Add("Times", 50, HorizontalAlignment.Left);
                listView1.Columns.Add("Memo", 50, HorizontalAlignment.Left);
      

  18.   

    wanyeye (助人者天助)        Blog 
    如果要在线程中为控件赋值请在构造函数中加上CheckForIllegalCrossThreadCalls = false;加哪里?加在类的构造函数中啊,晕倒!