在该控件上执行的操作正从错误的线程调用。使用 Control.Invoke 或 Control.BeginInvoke 封送到正确的线程才能执行此操作。
            this.Text += "-正在获取类别数据...";
            code = html.HtmlCode("http://news.google.cn/"); //获取google资讯页源码
            Regex r1 = new Regex("<a class=d href=\"(?<url>.+?)\">&nbsp;<font size=-1><b>(?<name>.{1,5})</b></font>&nbsp;</a>");    //取类别名和连接地址(链接地址需后期继续提取)的正则表达式
            MatchCollection m = r1.Matches(code);   //从源码中提取符合正则的字符串
            //遍历符合要求的字符串
            foreach (Match mmm in m)
            {
                TreeNode tn = new TreeNode();
                tn.Text = mmm.Groups["name"].ToString();
                Regex r2 = new Regex("(?<url>^/.+)\"\\>");  //取类别连接地址的正则
                tn.Tag = r2.Match(mmm.Groups["url"].ToString()).Groups["url"];  //设置tag为连接地址,方便取
                this.treeVtype.Nodes.Add(tn);    //这里报错了
            }
            this.Text = this.Text.Replace("-正在获取类别数据...","");
我主线程设置CheckForIllegalCrossThreadCalls = false;这个属性了

解决方案 »

  1.   

    最好不要CheckForIllegalCrossThreadCalls = false,这样很容易导致线程所请求资源的冲突,应该用form的invoke方法,
    参考
    http://blog.csdn.net/jinjazz/archive/2007/12/10/1927126.aspx
      

  2.   

    按楼上blog地址方法操作了,但这样好像无法达到多线程并发操作了。
    不知道是不是我写的有问题??       //使用invoke方法
            public delegate void dTest();
            void invokeThread()
            {
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Reset();
                sw.Start();
                if (this.InvokeRequired)
                {
                    this.Invoke(new dTest(SetTreeViewType));
                }
                else
                {
                    SetTreeViewType();
                }
                sw.Stop();
            }我在form_load里执行            getHtmlThread = new Thread(new ThreadStart(invokeThread));   //用线程设置树节点
                getHtmlThread.Start();
      

  3.   

    invoke是交还主线程来执行的,如果你很多线程都去调用
    this.Invoke(new dTest(SetTreeViewType));
    他们也是需要排队的
      

  4.   

     this.treeVtype.Nodes.Add(tn);    //这里报错了
    试试:TreeNode currentNode;    //当前要新增的节点
    delegate void DefaultDelegate(); //定义一个委托
    //...
    foreach (Match mmm in m)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = mmm.Groups["name"].ToString();
                    Regex r2 = new Regex("(?<url>^/.+)\"\\>");  //取类别连接地址的正则
                    tn.Tag = r2.Match(mmm.Groups["url"].ToString()).Groups["url"];  //设置tag为连接地址,方便取
                  currentNode = tn;
                  AddNodeToTree();
                  //.....private void AddNodeToTree()
    {
      try
      {
         if (this.InvokRequest)
          {
               this.Invoke(new DefaultDelegate(AddNodeToTree);
          }
          else
          {
             this.treeVtype.Nodes.Add(currentNode );
          }
      }
      catch
     {
     }
    }当然: