0

My query is about BackgroundWorker.

I have a windows forms application which starts 10 new threads. Each thread will get some info from 10 different web services. All I need is to append the result from web service call in a rich text box placed in the design mode. How can I make use of background thread in this scenario?

ArrayList threadList;

for (int idx = 0; idx < 10; ++idx)
{
    Thread newth= new Thread(new ParameterizedThreadStart(CallWS));
    threadList.Add(newth);       
}


for (int idx = 0; idx < 10; ++idx)
{
    Thread newth= new Thread(new ParameterizedThreadStart(CallWS));
    newth.Start(something);          
}


for (int idx = 0; idx < 10; ++idx)
{
    //Cast form arraylist and join all threads.
}

private void CallWS(object param)
{
    // Calling WS
    // got the response.
    // what should I do to append this to rich text box using background worker.
}

Any help much appreciated.

svick
  • 236,525
  • 50
  • 385
  • 514
Gonsal
  • 1
  • 1
  • 2
    It's 2011. Why are you using `ArrayList` instead of `List`? Also, your code doesn't make sense. Why are you creating 10 threads, then starting 10 completely different threads? – svick Aug 13 '11 at 12:59
  • The code doesn't use the BackgroungWorker component and the threading doesn't make sense. – Thilak Nathen Aug 13 '11 at 13:25

3 Answers3

1

In worker threads you can update richtextbox in following way:

private void CallWS(object param)
{
    string updatedText = ..

    // build a text    

    this.Invoke((MethodInvoker)delegate {
        // will be executed on UI thread
        richTextBox.Text = updatedText; 
    });
}
sll
  • 61,540
  • 22
  • 104
  • 156
  • Thanks, but program hangs on this line – Gonsal Aug 13 '11 at 10:12
  • I tried it before also. The Form becomes irresponsive.While debugging I can't see where the control is going after executing this line. – Gonsal Aug 13 '11 at 10:17
  • check this out : [http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the-t/142069#142069] – Saber Amani Aug 13 '11 at 10:25
  • I believe InvokeRequired always true when calling from an other thread, may I wrong – sll Aug 13 '11 at 10:39
  • @Gonsal: Try out richTextBox.Invoke() instead of this.Invoke – sll Aug 13 '11 at 10:40
  • Still in the same situation :-( – Gonsal Aug 13 '11 at 10:46
  • Oops I find out the problem. In my scenario. I had a Array list of 10 threads. In first loop I started them. In second loop I joined them.That join was causing the problem – Gonsal Aug 13 '11 at 10:58
  • @Gonal: lol :) Anyway happy to hear that you sorted it out. And anyway use Control.Invoke() to avoid issues when synchronizing with an UI thread – sll Aug 13 '11 at 10:59
1

I am not sure whether using BackgroundWorker is the best solution in your case. However, if you do use backgroundWorker, you could use the same RunWorkerCompleted event (which is run on the main thread) for all the BackgroundWorkers. So you could update your UI on that event.

If you are looking for an example for backgroundWorker look at here.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131
1

I don't really understand the context, but I believe the following:

  1. You are working with Windows.Forms
  2. You have multiple threads
  3. Each thread wants to execute code (appending text) in the UI thread

So the solution is not using a BackgroundWorker. Instead, you should use BeginInvoke: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.begininvoke.aspx

RichTextBox.BeginInvoke Method

Executes a delegate asynchronously on the thread that the control's underlying handle was created on.

Your problem, as Hans Passant commented on sllev's answer, could be you're blocking the UI thread for some reason, using Invoke.

Try replacing Invoke with BeginInvoke.

Community
  • 1
  • 1
paercebal
  • 81,378
  • 38
  • 130
  • 159