multithreading - accessing dynamically created form elements from a thread in vb.net -
i new vb , .net , have task unable proceed.
- i start thread when screen1 loads. user goes screen2.
- on screen2 thread still running behind. when user performs action(say click) on screen2, trigger thread access elements on screen2
- the elements in screen 2 dynamically created , not designed in ide.
so in essence thread created on 1 form needs access dynamically created form elements on form.
if question simple, please forgive me. if not clear please let me know , rephrase it.
note: element talking picture box inside flow layout panel.
thanks in advance eagerly(biting nails now) awaited , appreciated.
edit
lets thread called ctthread started in dashboard screen
ctthread.start()
this thread running endlessly, waiting trigger event.
meanwhile user has gone screen called quizscreen , on screen(form) have update dynamically created elements names know.
so when time right ctthread waiting (listener thread) call sub below.
sub
public sub changecomputerstatus(byval node) dim flowpanel flowlayoutpanel = ctype(quizscreen.flowlayoutpanel1.controls("flow_" + node), flowlayoutpanel) dim pictcontrol picturebox = ctype(flowpanel.controls("pict_" + node), picturebox) pictcontrol.image = system.drawing.image.fromfile(application.startuppath & "\images\application-on.png") end sub
here node keeps changing. how differentiate each control create.
note : thread started in screen called dashboardscreen , user on different screen quizscreen.
the first line of sub gave above runs , returns nothing flowpanel. hence when goes next line, not able use nothing reference. , hence above mentioned error.
two things.
the important thing must know cannot directly access control's properties background thread. controls may manipulated foreground ui thread. in order access controls background thread, need use form of asynchronous programming, e.g. creating delegates la .net 1.x - 3.5 or using new
task<t>
,async
,await
keywords.did add dynamically created controls form's control collection? mind you, you'll still need access controls via delegates or other asynchronous method explained in 1 above.
update:
to answer question in op's comment below: can invoke
method on ui object. basically, you're telling .net run invoked method , runs on ui object's creating thread (in case, ui thread), want. allow (depending on method or property invoked) "update" control "from background"—again, sleight of hand; when invoking method on ui object, invkoked method runs on ui thread possibly using data passed said method background task.
also, check out msdn documentation on bacgkroundworker
(this introduced in .net 2.0 , superseded async
, await
keywords along task<t>
in .net 4.5). there lots of documentation available explains how you're asking. it's not hard find performing quick search on msdn or bing (or preferred search engine).
Comments
Post a Comment