*Note:Rephrasing my question based on user's comments.
Need help on how to use .Net's Backgroundworker thread for the following purpose to increase UI resp开发者_StackOverflow社区onsiveness and performance in my .Net winforms application. When user clicks "Calculate" button in UI form,I'm doing this:
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.
FYI,I want to use Backgroundworker for steps #c and #d above.
I tried using Backgroundworker in my method.I put calls to InitializeBackgroundworker() and RunWorkerAsync() in the outermost loop[ie. step 2].But looks like, Backgroundworker is getting invoked only for the first category.What am I missing here?
So my question is, where do I make a call to InitializeBackgroundworker() and RunWorkerAsync()? And how do I make sure these 2 methods get called for each category?
Thanks for reading.
Try something like this for a start.
BackgroundWorker worker = new BackgroundWorker();
void SomeForm_Load(object sender, EventArgs e)
{
// setup the BackgroundWorker
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completed);
}
void SomeControl_Click(object sender, EventArgs e)
List<Category> categories = DataBase.GetSomeCategories(); // [1] get Category list
// start the BackgroundWorker passing in the categories list.
worker.RunWorkerAsync(categories);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int progress = 0;
var categories = e.Argument as List<Category>;
categories.ForEach(category => // [2] process each Category
{
ThirdPartyControl.DoSomeProcessing(category); // [2.a]
var categoryPrice = CalculateCategoryPrice(category);
var products = GetListOfProducts(category); // [2.b]
products.ForEach(product => // [2.c] process each Product
{
var productPrice = CalcProductPrice(categoryPrice);
DataBase.UpdateProduct(product, productPrice); // [2.d]
progress = //...calculate progress...
worker.ReportProgress(progress); // [3]
});
progress = //...calculate progress...
worker.ReportProgress(progress); // [3]
});
worker.ReportProgress(100);
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//...update some UI stuff...
progressBar.Value = e.ProgressPercentage;
}
void worker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
//...all done...
}
You can always break it into multiple BackgroundWorkers as your understanding increases.
See the example here:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
精彩评论