Possible Duplicate:
Most concise and proper way of avoiding cross thread operation error?
I got error when running my programs.... {"Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on."}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
TestObject argumentTest = e.Argument as TestObject;
string[] lines = argumentTest.ThreeValue.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
foreach (string vr in lines)
{
string country = argumentTest.OneValue.Trim();
string url = vr + country + '/code/' + argumentTest.TwoValue.Trim();
string sourceCode = WorkerClass.getSourceCode(url);
document.LoadHtml(sourceCode);
var title = document.DocumentNode.SelectSingleNode("//title");
var desc = document.DocumentNode.SelectSingleNode("//div[@class='productDescription']");
//-- eksekusi title
string isititle = title.InnerText;
string isititle2 = isititle.Replace("droidflashgame: ", "");
string isititle3 = Regex.Replace(isititle2, "[^A-Za-z0-9 ]+", "");
string isititle4 = isititle3.Substring(0, Math.Min(isititle3.Length, 120));
//-- Adding to list view for next step...
ListViewItem abg = new ListViewItem(isititle3);
abg.SubItems.Add(isititle4);
listView1.Items.Add(abg); // ERROR in Here?
I knew in some tutorial said using invoke? but I tried many thing but still error?
Any hand?
Try this .This works fine for me
ListViewItem abg = new ListViewItem(isititle3);
if (listView1.InvokeRequired)
listView1.Invoke(new MethodInvoker(delegate
{
listView1.Items.Add(abg);
}));
else
listView1.Items.Add(abg);
Remove last line (listView1.Items.Add(abg); // ERROR in Here?) from your code and replace it with this:
AddListViewItem(abg);
Then and this method to your code:
delegate void AddListViewItemDelegate(ListViewItem abg);
void AddListViewItem(ListViewItem abg)
{
if (this.InvokeRequired)
{
AddListViewItemDelegate del = new AddListViewItemDelegate(AddListViewItem);
this.Invoke(del, new object() { abg });
}
else
{
listView1.Items.Add(abg);
}
}
This will do the job, happy codding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With