日期:2014-05-17  浏览次数:20502 次

循环体内Invoke的问题
本帖最后由 u011497010 于 2013-11-19 15:40:09 编辑

由于用了多线程,下列方法会引起线程间操作无效,因此用invoke
但是在循环体内用invoke似乎比较麻烦
如下



 MatchCollection matches = rx.Matches(htmlSource);
    foreach (Match match in matches)
     {
     this.Invoke(new MethodInvoker(delegate() { lstViemItemCount = listView1.Items.Count; }));

       //这里开始循环
       for (int i = 0; i < lstViemItemCount; i++)
        {
        if (listView1.items[i].SubItems[1].Text.ToString() == match.Value.ToString())//listView1.items[i].SubItems[1].Text.ToString()这里要如何 Invoke????????
           {
            this.Invoke(new MethodInvoker(delegate() { tsslStatus.Text = "状态:目标已存在";  }));
             isExsit = true;
             continue;
            }
        }
  
}

// if (listView1.items[i].SubItems[1].Text.ToString() == match.Value.ToString())
//以上这句如何Invoke呢?

------解决方案--------------------
如果真要是有Invoke,可以使用
public Object Invoke(
Delegate method,
params Object[] args
)
args参数也支持ref,out方式

或者把listView1.items[i].SubItems[1].Text作为 List<string>参数不行么?