multithreading - About threads in c# -
i have button click contains ten methods. here want use threads in button click or in code windows form application not hang.
this have tried far...!!
collectlistofptags(); reqdoccheck = new thread(new threadstart(collectlistofptags)); reqdoccheck.isbackground = true; reqdoccheck.start(); waithandle[] await = new waithandle[] { new autoresetevent(false) }; while (reqdoccheck.isalive) { waithandle.waitany(await, 50, false); system.windows.forms.application.doevents(); }
in method collectionlistofptags()
getting exception says "a combobox accessed thread other created on"
thank's patience.. appreciated..
this looks fit backgroundworker
component.
split collectlistofptags
method 2 methods - first collects , processes data , second updates ui controls.
something this:
void onclick( ... ) { var results = new list<string>(); var bw = new backgroundworker(); bw.dowork += ( s, e ) => collectdata( results ); bw.runworkercompleted += ( s, e ) => updateui( results ); bw.runworkerasync(); } void collectdata( list<string> results ) { ... build strings , add them results list } void updateui( list<string> results ) { ... update combobox results }
the backgroundworker
run collectdata
in background on thread pool thread, run updateui
on ui thread can access combobox
correctly.
Comments
Post a Comment