在多线程应用程序中使用后台工作者

发布于 2024-11-29 04:16:53 字数 782 浏览 1 评论 0原文

我的查询是关于 BackgroundWorker 的。

我有一个 Windows 窗体应用程序,它启动 10 个新线程。每个线程将从 10 个不同的 Web 服务获取一些信息。我所需要的只是将 Web 服务调用的结果附加到设计模式下的富文本框中。在这种情况下如何利用后台线程?

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.
}

非常感谢任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哥,最终变帅啦 2024-12-06 04:16:53

在工作线程中,您可以通过以下方式更新 Richtextbox:

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

    // build a text    

    this.Invoke((MethodInvoker)delegate {
        // will be executed on UI thread
        richTextBox.Text = updatedText; 
    });
}

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; 
    });
}
爺獨霸怡葒院 2024-12-06 04:16:53

我不确定使用 BackgroundWorker 是否是您的情况的最佳解决方案。但是,如果您确实使用backgroundWorker,则可以使用相同的 RunWorkerCompleted事件(在主线程上运行)。因此您可以更新该事件的用户界面。

如果您正在寻找backgroundWorker的示例,请查看此处

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.

不可一世的女人 2024-12-06 04:16:53

我不太了解上下文,但我相信以下内容:

  1. 您正在使用 Windows.Forms
  2. 您有多个线程
  3. 每个线程都想在 UI 线程中执行代码(附加文本)

因此解决方案不是使用 BackgroundWorker。相反,您应该使用 BeginInvoke: http:// /msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.begininvoke.aspx

RichTextBox.BeginInvoke方法

在创建控件的基础句柄的线程上异步执行委托。

您的问题,正如Hans Passant评论sllev 的回答,可能是您阻塞了 UI 线程出于某种原因,使用 Invoke。

尝试用 BeginInvoke 替换 Invoke。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文