views:

32

answers:

3

I have a layered worker class that I'm trying to get progress reports from. What I have looks something like this:

public class Form1
{
    private void Start_Click()
    {
        Controller controller = new Controller();

        controller.RunProcess();
    }
}

public class Controller
{
    public void RunProcess()
    {
        Thread newThread = new Thread(new ThreadStart(DoEverything));
        newThread.Start();
    }

    private void DoEverything()
    {
        // Commencing operation...
        Class1 class1 = new Class1();

        class1.DoStuff();

        Class2 class2 = new Class2();

        class2.DoMoreStuff();
    }

}

public class Class1
{
    public void DoStuff()
    {
        // Doing stuff
        Thread.Sleep(1000);

        // Want to report progress here
    }
}

public class Class2
{
    public void DoMoreStuff()
    {
        // Doing more stuff
        Thread.Sleep(2000);

        // Want to report progress here as well
    }
}

I've used the BackgroundWorker class before, but I think I need something a bit more free form for something like this. I think I could use a delegate/event solution, but I'm not sure how to apply it here. Let's say I've got a few labels or something on Form1 that I want to be able to update with class1 and class2's progress, what's the best way to do that?

+1  A: 

Using events is the most straightforward solution. When you subscribe to the event from the main thread, the handler should check the Control.IsInvokeRequired to know whether it must call itself again through Invoke(...) to get the message passed to the right thread.

John Fisher
+1  A: 

John is correct. You want to utilize events and for that you'll need to use a delegate or delegates. This might give you some ideas.

http://www.yoda.arachsys.com/csharp/threads/winforms.shtml

Harv
A: 

If you do not want to block the processing threads during notification, you can use Control.BeginInvoke() for fire & forget behavior.

To decrease the number of calls and update progress on a regular interval, you may want to encapsulate the states of different operations in classes.
This way you can just write states to e.g. volatile fields - of presumably another, aggregate-state class - and use a timer on the GUI thread to re-read state and refresh labels accordingly.

andras