On Fri, 16 May 2008 10:26:01 -0700, auldh
<au***@discussions.microsoft.comwrote:
am having a hard time wrapping my head around "backgroundworker" class
and
updates to the window form that calls it.
It will be helpful if you first fully comprehend how Invoke() is used
absent a class like BackgroundWorker. Much of what BackgroundWorker does
is encapsulate the call to Invoke() so you don't have to deal with it. If
you don't understand Invoke(), it's hard to understand BackgroundWorker.
i have some questions about how to update windows form controls.
i have multiple labels and i'm using a text box for status updates.
the labels change when a task completes.
the text box report what task is running.
so
1) when i call _ProgressChange i need that function to passed the label i
want updating and what message?
What is "_ProgressChange"? How do you call it? There's no such method in
the BackgroundWorker class, so it's very difficult to know what you're
talking about.
If you really mean BackgroundWorker.ReportProgress(), which raises the
ProgressChanged event, then one overload of ReportProgress() takes an
Object as a parameter. This is copied to the
ProgressChangedEventArgs.UserState property. So you could pass the Label
you want updating, or a more complex class that references the Label and a
message string and anything else you want.
2) does _ProgressChange know how to update the label and text box or do i
have to defind other parameters to link the windows form control to the
_ProgressChange.
Again, what's "_ProgressChange"?
The BackgroundWorker class doesn't know anything about your own UI. All
it knows is to marshal the events ProgressChanged and RunWorkerCompleted
back to the thread on which the BackgroundWorker was created (if
possible...if you create it in a Forms application from the main GUI
thread, which is the usual use of the BackgroundWorker class, then it will
be possible). It is essentially calling Invoke() for you when raising the
ProgressChanged and RunWorkerCompleted events so that you don't have to.
You have to write all of the code to actually update your UI. That code
would go in your ProgressChanged event handler, and so of course you need
to make sure that all of the information that will be needed is passed at
the appropriate time, when you call BackgroundWorker.ReportProgress().
3) if i have many functions that start after each other and they give
status
do i need a different "backgroundworker" for each function? or can they
be
tied to one thread? (each function relays on the first one to finish
before
the next one starts).
Since the BackgroundWorker doesn't know anything at all about how you're
displaying progress, you can do it any way you like. If you want multiple
methods to be called from the same DoWork event of the same
BackgroundWorker, just call those methods from a single method that is
actually the DoWork event handler.
does anyone or has anyone come across a good complex sample of a
"backgroundwork" class with multiple _ProgressChange?
MSDN has sample code using BackgroundWorker. I recommend that you review
and learn that sample code well enough that you can discuss your use of
BackgroundWorker without using the wrong terminology. Once you're using
the correct terms and spelling for everything, it will be a lot easier to
understand your questions.
Pete