Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Changing ListView inside backgroundworker? Cross-Thread Error [duplicate]

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?

like image 488
radiaku Avatar asked Dec 21 '22 11:12

radiaku


2 Answers

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);    
like image 199
Karthik Avatar answered May 15 '23 18:05

Karthik


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!

like image 35
Gregor Primar Avatar answered May 15 '23 18:05

Gregor Primar