Reputation: 4278
I have a problem/question regarding background workers.
I am using VB.NET and EF5 in a win forms project. The main form has three tabs, each of which has a different set of data loaded into it. I am using the repository and Unit of Work patterns for all of my data access. To make the UI thread not lock I decided to use Background workers to populate each list after the UI loaded.
Initially in my code I had the following code in my forms loading method:
'//Set up tasks to obtain all the data from the DB and put it in the correct listviewitem format
AddHandler bgGetAllOpenOrders.DoWork, AddressOf BGAllOpenOrdersListGenerator
AddHandler bgGetAllNeedsAttention.DoWork, AddressOf BGAllNeedsAttentionOrdersListGenerator
AddHandler bgGetAllStock.DoWork, AddressOf BGStockListGenerator
'//Set up tasks to populate the relevant lists with the relevant data when all the data has been loaded.
AddHandler bgGetAllOpenOrders.RunWorkerCompleted, AddressOf BGPopulateAllOpenOrders
AddHandler bgGetAllNeedsAttention.RunWorkerCompleted, AddressOf BGPopulateNeedsAttentionList
AddHandler bgGetAllStock.RunWorkerCompleted, AddressOf BGPopulateStockList
'//Begin running of the loading code in the background whilst the form remains open to use.
bgGetAllOpenOrders.RunWorkerAsync()
bgAllNeedsAttention.RunWorkerAsync()
bgGetAllStock.RunWorkerAsync()
However, this would cause various problems such as not always returning all the correct data, other time I would get an error about the connection already being open (I tried activating MARS which didn't help)
So then I changed my on load method to the following:
'//Set up tasks to obtain all the data from the DB and put it in the correct listviewitem format
AddHandler bgGetAllOpenOrders.DoWork, AddressOf BGAllOpenOrdersListGenerator
AddHandler bgGetAllNeedsAttention.DoWork, AddressOf BGAllNeedsAttentionOrdersListGenerator
AddHandler bgGetAllStock.DoWork, AddressOf BGStockListGenerator
'//Set up tasks to populate the relevant lists with the relevant data when all the data has been loaded.
AddHandler bgGetAllOpenOrders.RunWorkerCompleted, AddressOf BGPopulateAllOpenOrders
AddHandler bgGetAllNeedsAttention.RunWorkerCompleted, AddressOf BGPopulateNeedsAttentionList
AddHandler bgGetAllStock.RunWorkerCompleted, AddressOf BGPopulateStockList
'//Begin running of the loading code in the background whilst the form remains open to use.
bgGetAllOpenOrders.RunWorkerAsync()
Inside the BGPopulateAllOpenOrders
I call bgAllNeedsAttention.RunWorkerAsync()
and within BGPopulateNeedsAttentionList
I call bgGetAllStock.RunWorkerAsync()
. This (I think) has a whiff of synchronicity about it but it works and the time difference between this and the old method is negligible, plus it always returns to the correct data and doesnt throw any errors.
What I want to know is, is this an anti-pattern/not recommended and can it cause any issues that I'm just not seeing?
Upvotes: 0
Views: 465
Reputation: 273179
It will work and it wont "cause any issues".
On the other had, it's not a very great pattern, ie hard to maintain.
Consider 1 Bgw that just runs 3 methods in sequence. I depends a little on what data comes back, and when/how to use that. But you can easily use Control.Invoke (Dispatcher.Invoke) to get back to the main thread, the Completed event just does that for you.
Upvotes: 1