I'm trying use thread to leech link from website, but when i trying run . It show error:
Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on
My code:
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);
}