后台工作人员返回值?

发布于 2024-10-20 14:53:41 字数 531 浏览 3 评论 0原文

以下代码将 1 到 100 之间的数字相加并返回总和。我想做的是在后台工作者中运行计算并返回一个值。这样做的问题是 returnValue 在 DoWork 完成之前返回。如何让它等待后台工作人员完成后再返回值? (我似乎无法将回报放入我的 DoWork...)

double returnValue = 0;

var b = new BackgroundWorker();
b.DoWork += new DoWorkEventHandler(
    delegate(object sender, DoWorkEventArgs e) {
        for(int i=0;i<100;i++){
            returnValue += (i+1);
        }
    }
);

b.RunWorkerAsync();
return returnValue;

附录:在同一线程上发送消息泵而不是在后台工作程序上运行它会更好吗?

另外,这只是示例代码,我的实际代码需要一分多钟才能完成。

The following code adds the numbers from 1 to 100 and returns the sum. What I'm trying to do is run the calculations in a backgroundworker and return a value. The problem with this is that returnValue is returned before DoWork completes. How can I have it wait for my background worker to complete before returning a value? (I can't seem to put the return in my DoWork...)

double returnValue = 0;

var b = new BackgroundWorker();
b.DoWork += new DoWorkEventHandler(
    delegate(object sender, DoWorkEventArgs e) {
        for(int i=0;i<100;i++){
            returnValue += (i+1);
        }
    }
);

b.RunWorkerAsync();
return returnValue;

Addendum: Would it be better to send a message pump on the same thread instead of running this on a background worker?

Also, this is just example code, my actual code takes more than a minute to complete.

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

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

发布评论

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

评论(5

我不在是我 2024-10-27 14:53:41

订阅 RunWorkerCompleted< /a> 事件。该事件包含后台操作的返回值。

当然,该值将从 DoWorkEventHandler 内部返回,如下所示:

b.DoWork += new DoWorkEventHandler(
    delegate(object sender, DoWorkEventArgs e) {
        double returnValue = 0;
        for(int i=0;i<100;i++){
            returnValue += (i+1);
        }
        e.Result = returnValue;
    }
);

Subscribe to the RunWorkerCompleted event. That event contains the return value of the background operation.

Of course, that value would be returned from inside the DoWorkEventHandler, like so:

b.DoWork += new DoWorkEventHandler(
    delegate(object sender, DoWorkEventArgs e) {
        double returnValue = 0;
        for(int i=0;i<100;i++){
            returnValue += (i+1);
        }
        e.Result = returnValue;
    }
);
叹沉浮 2024-10-27 14:53:41

我在这里并没有真正看到问题,但我认为您正在寻找的是名为 RunWorkerCompleted 的事件。当 DoWork 委托完成时会引发该异常。如果这不是您想要的,我认为您需要重新表述您的问题。

I don't really see a question here, but what I think you are looking for is the event called RunWorkerCompleted. That gets raised when the DoWork delegate completes. If this is not what you are looking for, I think you need to rephrase your question.

泼猴你往哪里跑 2024-10-27 14:53:41

我发布了一个示例:此处
也许它可以帮助你:-)

另外,这只是示例代码,我的实际代码需要一分多钟才能完成。

这可能是异步启动的效果。您可以告诉后台工作人员何时开始或直接说开始。如果你没有明确地说他应该现在就开始,那么他会在 c# 认为现在是开始的好时机时开始:-)

i have postet a sample: here
maybe it can help you :-)

Also, this is just example code, my actual code takes more than a minute to complete.

this can be the effect of the async start. you can tell the backgroundworker when to start or just say start. if you don´t explicit say that he should start NOW he starts when c# thinks it is a good time to start :-)

避讳 2024-10-27 14:53:41

如果像我一样,您无法使用接受的答案,因为 DoWorkEventHandler 返回 void,下面的代码可能会有所帮助。

double returnValue = 0;
var b = new BackgroundWorker();
b.DoWork += PerformJob;
b.RunWorkerCompleted += JobDone;

private void PerformJob(object sender, DoWorkEventArgs args)
{
    // Pre-Processing: for-loop to increment returnValue
    /*
    ** DoWorkEventArgs has a 'Result' object with the following description
    ** Summary: Gets or sets a value that represents the result of an asynchronous operation.
    ** Returns: An System.Object representing the result of an asynchronous operation.
    */
    args.Result = returnValue;
}

private void JobDone(object sender, RunWorkerCompletedEventArgs args)
{
    //RunWorkerCompletedEventArgs also has a 'Result' object like DoWorkEventArgs
    double updatedReturnValue = (double)args.Result;

    // Post-processing: e.g. use updatedReturnValue to update a text block, show a message box etc.
}

If, like me, you were not able to use the accepted answer because DoWorkEventHandler returns void, the code below might help.

double returnValue = 0;
var b = new BackgroundWorker();
b.DoWork += PerformJob;
b.RunWorkerCompleted += JobDone;

private void PerformJob(object sender, DoWorkEventArgs args)
{
    // Pre-Processing: for-loop to increment returnValue
    /*
    ** DoWorkEventArgs has a 'Result' object with the following description
    ** Summary: Gets or sets a value that represents the result of an asynchronous operation.
    ** Returns: An System.Object representing the result of an asynchronous operation.
    */
    args.Result = returnValue;
}

private void JobDone(object sender, RunWorkerCompletedEventArgs args)
{
    //RunWorkerCompletedEventArgs also has a 'Result' object like DoWorkEventArgs
    double updatedReturnValue = (double)args.Result;

    // Post-processing: e.g. use updatedReturnValue to update a text block, show a message box etc.
}
前事休说 2024-10-27 14:53:41

您正在尝试与BackgroundWorker 进行同步操作,这是。但如果必须的话,您可以使用 IsBusy 标志。

double returnValue = 0;

var b = new BackgroundWorker();
b.DoWork += new DoWorkEventHandler(
    delegate(object sender, DoWorkEventArgs e) {
        for(int i=0;i<100;i++){
            returnValue += (i+1);
        }
    }
);

b.RunWorkerAsync();
while(b.IsBusy){
  Application.DoEvents();
}
return returnValue;

You are trying to do a synchronous operation with BackgroundWorker which it BAD. But if you must, you could use the IsBusy flag.

double returnValue = 0;

var b = new BackgroundWorker();
b.DoWork += new DoWorkEventHandler(
    delegate(object sender, DoWorkEventArgs e) {
        for(int i=0;i<100;i++){
            returnValue += (i+1);
        }
    }
);

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