Painting in a Thread

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

I have a windows form that paints part of the screen by going away to a web
service to get some information. However, the first time it tries this there
is a delay of a few seconds, preventing the user from interacting with the
form.

What I think I need to do is to create a new Thread for the webservice
lookup. How can I do this in such a way that I still have the form's Control
collection available for painting within the thread when the lookup has
returned data?
 
JezB said:
I have a windows form that paints part of the screen by going away to a web
service to get some information. However, the first time it tries this there
is a delay of a few seconds, preventing the user from interacting with the
form.

What I think I need to do is to create a new Thread for the webservice
lookup. How can I do this in such a way that I still have the form's Control
collection available for painting within the thread when the lookup has
returned data?

You *must not* paint within a thread other than the one which "owns"
the control you're painting onto. Instead, use
Control.Invoke/BeginInvoke.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

And you're right - you certainly shouldn't be doing the web service bit
in the UI thread.
 
Back
Top