Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}
Posted
Updated 2-Feb-11 16:14pm
v3

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-11 23:03pm    
Basically, I agree (a 5); see also my answer.
--SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Feb-11 22:14pm    
That is correct - my 5.
--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
 
Share this answer
 
v3
Comments
Espen Harlinn 2-Feb-11 15:53pm    
Well done, good tip, 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900