ThreadPool Threads and UI

J

John Puopolo

All,

I just watched a webcast where the presenter showed performing some
async work via using ThreadPool.QueueUserWorkItem(...). This call
uses thread from the .NET process thread pool to do some work in a
worker function. So far, so good.

In the webcast, the code in the worker function (the one the thread
from the thread pool runs in) directly updates the WinForm's UI (calls
update function on a progress bar control).

I've been programming in Win32 for longer than I care to admit --- and
this is generally a BAD THING to update a UI from any thread other
than the UI thread that created the control.

So, the questio is... Do the threads in the thread pool/the thread
pool itself do something special vis-a-vis interacting with the UI or
did the webcast's author overlook a potiential pitfall?

Thanks,
John
 
P

Patrick Steele [MVP]

In the webcast, the code in the worker function (the one the thread
from the thread pool runs in) directly updates the WinForm's UI (calls
update function on a progress bar control).

I've been programming in Win32 for longer than I care to admit --- and
this is generally a BAD THING to update a UI from any thread other
than the UI thread that created the control.

So, the questio is... Do the threads in the thread pool/the thread
pool itself do something special vis-a-vis interacting with the UI or
did the webcast's author overlook a potiential pitfall?

How was the update done? Did the author directly call methods on the
progress bar, or did he use the "BeginInvoke" method?

The "BeginInvoke" method will marshal the call to the UI thread for you
so if he did it that way, then it's okay. If he just grabbed the
"Value" property and incremented it, then yeah -- he overlooked a
potential pitfall.
 
J

John Puopolo

Hi... Thanks for the quick reply.

Yeah, the he accessed the value directly. He did not go thru the
BeginInvoke which would have gone thru the proper sequence.

John
 
J

Jon Skeet [C# MVP]

John Puopolo said:
Yeah, the he accessed the value directly. He did not go thru the
BeginInvoke which would have gone thru the proper sequence.

Then it would be worth giving feedback about that. Your instincts are
quite correct - it is indeed a bad thing to update the UI from another
thread.
 
J

John Puopolo

Jon Skeet said:
Then it would be worth giving feedback about that. Your instincts are
quite correct - it is indeed a bad thing to update the UI from another
thread.

I tried contacting the author, but my e-mails bounced (I guessed at
his e-mail addresses from his name and company --- different combos).
Is there a place on the MS/MSDN web site where I can give feedback?
In total, the Webcast was decent and informative -- this was a the one
egregious error. Thanks.

John
 
J

Jon Skeet [C# MVP]

John Puopolo said:
I tried contacting the author, but my e-mails bounced (I guessed at
his e-mail addresses from his name and company --- different combos).
Is there a place on the MS/MSDN web site where I can give feedback?
In total, the Webcast was decent and informative -- this was a the one
egregious error. Thanks.

I don't know, I'm afraid... I'd have a look around the webcast page and
see if there's anything there, if I were you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top