multithreading - Threading & Cross Threading in C#.NET, How do I change ComboBox Data from another Thread? -
i need use threading in app, don't know how perform cross threading operation.
i want able change text of form object (in case combo box), thread, error:
cross-thread operation not valid: control 'titlescombobox' accessed thread other thread created on. i don't understand how use invoke , begin invoke functions, im looking dead simple example , explanation can learn around that.
also beginner tutorials great, found few, different, don't understand need perform cross threading ops.
here code:
// main thread. on click of refresh button private void refreshbutton_click(object sender, eventargs e) { titlescombobox.items.clear(); thread t1 = new thread(updatecombo); t1.start(); } // function updates combo box rssdata private void updatecombo() { rssdata = getrssdata(channeltextbox.text); // getting data (int = 0; < rssdata.getlength(0); i++) // output { if (rssdata[i, 0] != null) { // cross-thread operation not valid: control 'titlescombobox' // accessed thread other thread created on. titlescombobox.items.add(rssdata[i, 0]); // here error } titlescombobox.selectedindex = 0; } }
i use following helper class:
public static class controlextensions { public static void invoke(this control control, action action) { if (control.invokerequired) { control.invoke(new methodinvoker(action), null); } else { action.invoke(); } } } now can call mycombo.invoke(() => { mycombo.items.add(something); }) --- or other control (such form) before invoke since created on main thread.
the thing controls can accessed thread created on (the main application thread in case).
hth
Comments
Post a Comment