我想使用線程從網站水蛭鏈接,但是當我嘗試運行。它顯示錯誤:c# - 跨線程操作無效ListView
Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on
我的代碼:
try
{
foreach (HtmlNode node in (IEnumerable<HtmlNode>)document.DocumentNode.SelectNodes("//table[@class='tbl' and @id='stats']//tr[@class='' or @class='bg']"))
{
HtmlAgilityPack.HtmlDocument document2 = new HtmlAgilityPack.HtmlDocument();
document2.LoadHtml(node.InnerHtml);
try
{
string str6 = document2.DocumentNode.SelectSingleNode("//td[2]//a").Attributes["href"].Value;
string innerText = document2.DocumentNode.SelectSingleNode("//td[2]//a").InnerText;
string[] items = new string[] { listView1.Items.Count + 1.ToString(), innerText, str6, "" };
ListViewItem item = new ListViewItem(items);
listView1.Items.Add(item);
listView1.EnsureVisible(listView1.Items.Count - 1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
''InvokeRequired''成員屬性是你的朋友;) – BitTickler
如何添加InvokeRequired? –
縮進也是你的朋友。請。 –