views:

907

answers:

4

The Scenario

I have a windows forms application containing a MAINFORM with a listbox on it. The MAINFORM also has a THREAD POOL that creates new threads and fires them off to do lots of different bits of processing. Whilst each of these different worker threads is doing its job, I want to report this progress back to my MAINFORM, however, I can't because it requires Cross-Thread communication.

Progress

So far all of the tutorials etc. that I have seen relating to this topic involve custom(ish) threading implementations, whereas I literally have a fairly basic(ish) standard THREAD POOL implementation. Since I don't want to really modify any of my code (since the application runs like a beast with no quarms) - I'm after some advice as to how I can go about doing this cross-thread communication. ALTERNATIVELY - How to implement a different "LOGTOSCREEN" method altogether (obviously still bearing in mind the cross-thread communication thing).

WARNING:

I use this website at work, where we are locked down to IE6 only, and the javascript thus fails, meaning I cannot click accept on any answers during work, and thus my acceptance rate is low. I can't do anything about it I'm afraid, sorry.

EDIT:

I DO NOT HAVE INSTALL RIGHTS ON MY COMPUTER AT WORK. I do have firefox but the proxy at work fails when using this site on firefox. And no, funnily enough, I don't have the internet at home, I literally just moved to this city and the flat is a new build, so the address hasn't been registered with the post office, and thus the phone company cannot find the address on their system till they send a surveyor out, smarty pants.

FURTHER EDIT:

I DO NOT WANT TO CHANGE MY THREADING IMPLEMENTATION. AT ALL! - Accept to enable cross-thread communication....why would a backgroundworker help here!?

CODE RELATED EDIT:

Does it make a difference that when my THREAD POOL executes the new threads, it creates a new instance of a class and calls the entire thing on that new thread........i.e. your code example doesn't quite fit....i think?

+3  A: 

Use the BackgroundWorker class in .NET and use the ProgressChanged and RunWorkerCompleted events to communicate back to your UI thread

Edit:

Sounds like you don't like BackgroundWorker, or just don't want to refactor. In that case, you have to check the InvokeRequired property on your form or one of your controls and if it is true, then you have to call Control.Invoke to force your UI update logic to occur on your main thread.

here is an example:

private void MyThreadFunction()
{
    if (!InvokeRequired)
    {
        myLabel.Text = "You pushed the button!";
    }
    else
    {
        Invoke(new ThreadStart(MyThreadFunction));
    }
}

You can use any delegate type to pass to Invoke, because it takes optional parameters that can be passed to your delegate when it is invoked on the main thread.

mjmarsh
A: 

You can do this using delegate object. So you would create a callback method in your MAIN form and let your CHILD forms call this method using delegates when they are done processing.

SoftwareGeek
A: 

Try using Control.BeginInvoke to queue your update to the UI on the UI thread.

gilbertc
+1  A: 

You could do something like this:

class MyForm : Form
{
    private Label label = new Label();

    private void DoWork()
    {
        // Do work ... Not in UI thread

        // Update label... In UI thread
        this.Invoke(new MethodInvoker(() => label.Text = "New Text!"));
    }
}

The DoWork method it's the one running in your worker threads. You could check if an invoke is required using InvokeRequired property, but the assumption is that your code is running on worker threads so the invoke will always be required.

João Angelo