BackgroundWorker完成后处理另一个事件 [英] BackgroundWorker Process the other event after finish

查看:92
本文介绍了BackgroundWorker完成后处理另一个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要BackgroundWorker忙时,希望在消息队列存储之类的地方处理的事件,在释放BackgroundWorker之后执行.


I want when BackgroundWorker is busy, events that wish to handle in the place like message queue store, after release BackgroundWorker they execute.


private void btnAdd_Click_1(object sender, EventArgs e)
{
    btnCancel.Enabled = true;
    if (Worker.IsBusy)
        return;          //events can be store,until after to be execute

    if(!Worker.IsBusy)
    Worker.RunWorkerAsync(numericUpDown1.Value);
}

推荐答案

首先,我认为您应该考虑重新设计逻辑.将事件存储在队列中会否认BackgroundWorker的目的,因为BackgroundWorker通常用于使UI保持响应...

无论如何,我认为您将必须实现自己的事件队列,但这不应该太困难.创建一个简单的类来表示事件并存储所有必要的信息,然后处理RunWorkerCompleted事件以处理队列中的所有项目.
First of all, I think you should consider re-designing your logic. Storing events in a queue denies the purpose of BackgroundWorker, since BackgroundWorker is typically used to keep the UI responsive...

Anyway, I think you will have to implement you own event queue, but it shouldn''t be too difficult. Create a simple class that will represent an event and store all the necessary information, then handle the RunWorkerCompleted event to process all the items in your queue.


我不明白你想做.

如果要在BackgroundWorker完成后执行某些操作,只需处理BackgroundWorker.RunWorkerCompleted事件.

否则,请提供更多详细信息和更多代码,以便我们为您提供更好的帮助.
I don''t understand what you want to do.

If you want to execute something after the BackgroundWorker finishes, just handle the BackgroundWorker.RunWorkerCompleted event.

Otherwise, please give more details and more code so we can help you better.


有关事件队列"的信息,请参见我的技巧/窍门文章:

用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

我的其他答案中显示了如何使用BackgroundWorker的合理示例:
逐步更改以显示控件 [ ^ ].

关于完成BackgroundWorker任务的过程,请参阅Olivier的回答.

您将从我的示例中看到线程技术;我测试了另请参阅页面上的备用提示,但这仅适用于Framework v.4.0.我的代码有什么好处,它也适用于2.0.

—SA
For "event queue" see my Tip/Tricks article:

Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

A reasonable sample of how to work with BackgroundWorker is shown in my other answer:
Changes step by step to show a control[^].

On process of completion of the task of BackgroundWorker please see the answer by Olivier.

You will see thread technique from my sample; I tested it. See also the alternative Tip on the page, but that''s only for v.4.0 of the Framework. What''s good about my code, it works with 2.0 as well.

—SA


这篇关于BackgroundWorker完成后处理另一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆