Threads Question - updating UI

  • Thread starter Thread starter Flashster
  • Start date Start date
F

Flashster

I'm new to all this so please forgive me if I use the wrong terms or have
used the wrong approach here...

I have a thread which gathers some information from the internet for my
windows application. It gathers the information fine, but I am having a
problem when trying to display it.

The information is being diplayed in a datagrid, and has about 100 rows.
When the thread throws an "Information Has Been Gathered" event (which would
essentially be just before the thread has finished) to my main windows app I
try to display the information passed in the datagrid.

However, the main program throws a 'null exception' at fairly random points
during the populating of the datagrid. Sometimes it may throw it whilst
displaying row 47, sometimes at row 22, etc.. It seems to me that the thread
is finishing before it can complete the UI displaying tasks in the
"Information Has Been Gathered" event handler routine. I have tried just
getting it to diplay a couple of lines and it works ok.

The only way I can get it to work is if I tell the main application to wait
for the thread (i.e. thread1.join) before populating the datagrid. This is
clearly what I didn't want to do as the program then freezes until the
thread has finished.

How should I do this? Am I using the right approach? Should I try using a
delegate to update the UI from the thread, or will this still not solve the
problem? (not used delegates before so new ground for me again)

TIA
 
Thanks Alan - excellent articles. Nearly understand what I'm doing now.
I'll keep at it.
 
Back
Top