Backgroundworker Control

D

dm1608

Hi --

I have a C# application that basically has a button that executes a SQL
Reader to loop thru a rather large resul set. Thru each interation of the
reader object, I check to see if a file exists on the server. If it does, I
update a textbox (multiline) with the missing file, update a status bar
label, and a progressbar. Since this process is kind of lengthy, every 400
rows, I do a DoEvents() and TextBox.Refersh() to refresh the form and
control... so I can see the updated info in the textbox.

This seems to work fine and is OK, but I thought that using the new
Backgroundworker control would make my UI more responsive and I could likely
remove the DoEvents and junk.

Anyway, from what I've seen thus far... it looks like the DoEvent function
of the backgroundworker control does not allow me to interact with any UI
controls. I see that I can report the progress back to my ProgressEvent,
but that appears to be all.

What is the best practice for handling this sort of scanerio?

Surely I can't be the only one that has wanted to run a query in the
background, update some controls, and finally release control back to the
main UI?????
 
W

William Stacey [MVP]

I personally don't find the background worker all that helpful as it is
easier, IMO, to do it yourself. Try this:
http://channel9.msdn.com/ShowPost.aspx?PostID=171594

--
William Stacey [MVP]

| Hi --
|
| I have a C# application that basically has a button that executes a SQL
| Reader to loop thru a rather large resul set. Thru each interation of the
| reader object, I check to see if a file exists on the server. If it does,
I
| update a textbox (multiline) with the missing file, update a status bar
| label, and a progressbar. Since this process is kind of lengthy, every
400
| rows, I do a DoEvents() and TextBox.Refersh() to refresh the form and
| control... so I can see the updated info in the textbox.
|
| This seems to work fine and is OK, but I thought that using the new
| Backgroundworker control would make my UI more responsive and I could
likely
| remove the DoEvents and junk.
|
| Anyway, from what I've seen thus far... it looks like the DoEvent function
| of the backgroundworker control does not allow me to interact with any UI
| controls. I see that I can report the progress back to my ProgressEvent,
| but that appears to be all.
|
| What is the best practice for handling this sort of scanerio?
|
| Surely I can't be the only one that has wanted to run a query in the
| background, update some controls, and finally release control back to the
| main UI?????
|
|
|
 
K

Kevin Spencer

If the BackgroundWorker had access to the Controls, it would have the same
problem as any other separate thread in a Windows Form. That is, since the
work of the BackgroundWorker is conducted on a separate thread from the main
form thread, accessing Form controls is a dangerous threading risk to take.
That is why the BackgroundWorker raises an event that the Form thread itself
can respond to.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 

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