views:

68

answers:

1

Hi,

Need help on how to use .Net's Backgroundworker thread for the following purpose to increase UI responsiveness and performance in my .Net winforms application.Since am new to .Net/C#, any code would be helpful.

When user clicks "Calculate" button in UI form,

1.Get a list of categories C from database[this is typically around 10]

2.For each category C, do the following:

a.Call a third-party library, do some processing and calculate category price.

b.Get a list of products[This is typically around 800].

c.For each product, calculate its price using its category price from above.

d.Update this price of each product back in the database using a stored proc.

3.Update the progress back[or report any error message] to a form in UI.

I want to use Backgroundworker for steps #c and #d above. So, where do I make a call to InitializeBackgroundworker and RunWorkerAsync?

Also, how do I make sure these 2 methods get called for each category?

Thanks for reading.

A: 

Doing this in .NET is pretty simple - just write a method that will do the processing you want (and make sure it doesn't touch anything on the UI), and then pass that function to the background worker - or directly to

ThreadPool.QueueUserWorkItem(methodName)

Example function

function void methodName(object state)
{
    // Go get your Categories

    // Loop through Categories

    // Call third party library

    // Get list of products

    // Calculate price and update database
}

The BackgroundWorker does make it a little easier to update the UI from a background thread, but you can handle this your self by using the ISynchronizeInvoke interface.

davisoa