loops - C# - If I use backgroundWorker nothing is displayed in the treeView control -


i populate array "numbers" in "treeview1" control. works if don't use backgroundworker control.

if use backgroundworker nothing displayed in treeview1. looks code stuck in foreach loop.

enter image description here

        //step1     private void button1_click(object sender, eventargs e)     {                   checkforillegalcrossthreadcalls = false;         backgroundworker1.runworkerasync();         button1.enabled = false;     }      //step2     private void backgroundworker1_dowork_1(object sender, doworkeventargs e)     {         int[] numbers = new int[5];         numbers[0] = 1;         numbers[1] = 2;         numbers[2] = 3;         numbers[3] = 4;         numbers[4] = 5;          foreach (int element in numbers)         {             treeview1.nodes.add(element.tostring());         }         button1.enabled = true;     } 

i tried suggested below , still doesn't display anything

        //step1     private void button1_click(object sender, eventargs e)     {          checkforillegalcrossthreadcalls = true;         backgroundworker1.workerreportsprogress = true;         backgroundworker1.workersupportscancellation = true;         backgroundworker1.runworkerasync();         button1.enabled = false;     }      //step2     private void backgroundworker1_dowork_1(object sender, doworkeventargs e)     {         this.treeview1.nodes.clear();          int[] numbers = new int[5];         numbers[0] = 1;         numbers[1] = 2;         numbers[2] = 3;         numbers[3] = 4;         numbers[4] = 5;         treeview1.beginupdate();         foreach (int element in numbers)         {             treeview1.nodes.add(element.tostring());         }         treeview1.endupdate();         button1.enabled = true;         treeview1.refresh();     } 

thanks banjamin work :)

        //step1     private void button1_click(object sender, eventargs e)     {          checkforillegalcrossthreadcalls = false;         backgroundworker1.workerreportsprogress = true;         backgroundworker1.workersupportscancellation = true;         backgroundworker1.runworkerasync();         button1.enabled = false;     }      //step2     private void backgroundworker1_dowork_1(object sender, doworkeventargs e)     {         this.treeview1.nodes.clear();          int[] numbers = new int[5];         numbers[0] = 1;         numbers[1] = 2;         numbers[2] = 3;         numbers[3] = 4;         numbers[4] = 5;          treeview1.invoke((action)delegate         {             foreach (int element in numbers)             {                 treeview1.nodes.add(element.tostring());             }         });          treeview1.endupdate();         //button1.enabled = true;         treeview1.refresh();         system.windows.forms.messagebox.show("completed");         button1.invoke((action)delegate { button1.enabled = true; });     } 

if run code in debugger exception popup giving more details on going wrong:

additional information: action being performed on control being called wrong thread. marshal correct thread using control.invoke or control.begininvoke perform action.

you should update ui main (or window) thread of application. correct code is:

private void backgroundworker1_dowork(object sender, doworkeventargs e) {     int[] numbers = new int[5];     numbers[0] = 1;     numbers[1] = 2;     numbers[2] = 3;     numbers[3] = 4;     numbers[4] = 5;      treeview1.invoke((action)delegate      {        foreach (int element in numbers)        {            treeview1.nodes.add(element.tostring());        }    });     button1.invoke((action) delegate { button1.enabled = true; }); } 

by using invoke method on treeview1 , button1 ensure correct thread used update ui.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -