Re: Mulitihreading problem with BackgroundWorker

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • Re: large files: when ubiquitous?
    ... >> The fact that undefined behaviour is invoked is an immediate consequence ... If your program complies with the rules of the C standard, ... branches are completely equivalent and both invoke undefined behaviour ... As with every transformation a compiler does, ...
    (comp.os.linux.development.system)
  • Re: Custom keywords and compiler-supplied code
    ... Having to sprinkle GUI code with InvokeRequired tests means the GUI ... I.e. the C# compiler and JIT compiler between them have ... Regarding the runtime handling of a delegate's Invoke method, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Java VM Address Space
    ... By everyone else's definition of the word "compile" Java bytecode is ... A second stage of the compiler ... which mostly resemble the set written in the Java language. ... the four invoke opcodes could be said to ...
    (comp.lang.java.programmer)
  • Re: sizeof operator implementation
    ... sizeofat compile time. ... How does C compiler gets value at ... it definitely invokes UB due to casting 10 to a pointer type; might as well cast 0 and make what you're doing obvious. ... Casting 10 to a pointer type does *not* invoke UB, ...
    (comp.lang.c)
  • Re: The C Programming Language: Third Edition
    ... the compiler in C90 conforming mode for portability reasons. ... Frankly thats a silly point to raise when claiming the compiler ... You could as well say it doesn't support ... Of /course/ it doesn't, if you don't invoke ...
    (comp.lang.c)