Re: Mulitihreading problem with BackgroundWorker
- From: "Jan" <janjohanssen@xxxxxxxxxxxxxxx>
- Date: Fri, 18 Dec 2009 15:20:43 -0500
Positions is a class and the Start method is the entry point to a financial
analysis program that uses 12 different programs in three classes, so in the
interest of brevity, I didn't post more. The program can take 10-20 minutes
to complete and runs through many iterations of different scenarios.
All of the examples I have seen in my research show everything happening in
the Form class. I pass a reference to the main form to the Positions class
(Positions.Start(this)) so that the Positions class can reference data on
the form (we can't use the "this" keyword in a class separate from the Form
class). Now that Positions runs in a separate thread I can't see how to
address a label or a text box residing on the main form.
In the examples I have seen, all of the code is contained within the DoWork
method, and ReportProgress is called from within DoWork. However, in my
code, DoWork must call Positions, which is in another class. In brief, it
looks like this:
public partial class Positions
{
public static void Start (Form1 frm)
{
.. . .
// Here I would like to update frm.label1.text but I get a compiler error
that it was created on a different thread.
// I have tried the Invoke method on the label, but now the compiler doesn't
recognize it for the same reason.
}
So the problem comes when we go into a class separate from the Form class.
If, given that, you can shed any more light on this I would be grateful.
Thanks again.
"Peter Duniho" <no.peted.spam@xxxxxxxxxxxxxxxxxx> wrote in message
news:uC1RBYBgKHA.2188@xxxxxxxxxxxxxxxxxxxxxxx
Jan wrote:
[...]
Within the class that executes the long-running process, I have tried
several ways of invoking the delegate that updates the text, but none
will
compile correctly.
Given this scenario, how can I get the label text property to update from
a
class running on a different thread?
Under what conditions do you want the text to be updated? If it's when
the progress is reported by your background worker task, then IMHO the
easiest thing to do is pass the string to the ReportProgress() method as
the second argument. Then your ProgressChanged event handler can get the
reference via the ProgressChangedEventArgs.UserState property, cast it
back to a string, and assign to the appropriate Text property.
If that's not helpful, you need to provide a more complete code example.
There's nothing in the example you've shown that explains what
"Positions.Start(this)" does or why it's relevant to the example at all,
nor shows how you attempted to invoke the callback you tried to declare or
in what situation you want to invoke the delegate.
Pete
.
- Follow-Ups:
- Re: Mulitihreading problem with BackgroundWorker
- From: Peter Duniho
- Re: Mulitihreading problem with BackgroundWorker
- References:
- Mulitihreading problem with BackgroundWorker
- From: Jan
- Re: Mulitihreading problem with BackgroundWorker
- From: Peter Duniho
- Mulitihreading problem with BackgroundWorker
- Prev by Date: Re: Delegates and Events
- Next by Date: Re: Can't listen the event MouseWheel
- Previous by thread: Re: Mulitihreading problem with BackgroundWorker
- Next by thread: Re: Mulitihreading problem with BackgroundWorker
- Index(es):
Relevant Pages
|