vb.net Loop though typed bound datatable on backgroud worker

Hi I have a typed datatable filled from sql server, bound to 2 gridviews
I need to loop through each row every 5 mins and check if price has changed, if so update it. (the price is coming from an external server)

I did have this on a timer and then loop through each row on same thread but was freezing the UI well it was looping, so now have a background worker to check and update.

the only way i seem to be able to get it to work is to clone the table then loop through rows then on background work completed fill the table again. There must be another way.
If I dont clone the table first the ui freezes and does not recover. Please help.
LVL 3
taz8020Asked:
Who is Participating?
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Connect With a Mentor Infotrakker SoftwareCommented:
You don't have to "stop" the binding. Just grab a clone of the datatable, pass it to the BGW, allow the BGW to do the update, then rebind.
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerCommented:
Have you started the timer inside the background worker?
0
 
taz8020Author Commented:
no the timer calls the background worker as follows:
if backgroundworkerCheckData.isbusy = false then
backgroundworker.runworkerasync()
End if
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerCommented:
In-order for an app to not freeze the GUI you should be calling the timer inside the backgroundworker then your form won't freeze while the timer is running.
0
 
taz8020Author Commented:
but the timer says to run the code in the background worker.

So how would i start and do work with timer in a background worker?
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerCommented:
I did have this on a timer and then loop through each row
you need to change to this and on your formload start the backgroundworker and inside the doWork of the backgroundworker you need to call the timer(x).start() and your timer interval should be adjusted so that it will only fire after 5 min as you wish.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I would disagree with running the timer in the BGW. Your timer should fire off the BGW and nothing else. After all, the TIMER is not the reason your UI became unresponsive - it was the process of updating your datatable (I'd think).

I'd use the timer, and then start the BGW in that time loop. Better still, create a Class that runs the BGW and fires Events when it's done with the update. You'd pass in the Datatable, start the BGW, and your class could alert the Form when it's finished by raising an event (which you've sinked in the form). when that Form event fires, you could retrieve the datatable from the class and use it to refresh/rebind the grids.
0
 
taz8020Author Commented:
Hi Scott, I agree i think the problem is because the datatable is bound to 2 gridviews while they are updating. How do i stop/pause the binding without saying datasoure is null?
0
 
taz8020Author Commented:
so something like this

'in timer
dim clonetable as new mydataset.table
clonetable  = .w.mydataset.table

'in background worker
for each row in clonetable
'do update
next

'in complete
me.datagrid.datasorce = clonetable

is that right?
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Actually:

Dim CloneTable as New Datatable
CloneTable = MyDataset.Table(0)

Or, assuming your DataGridView is based on a Datatable:

CloneTable = YourDataGridView.DataSource
0
 
taz8020Author Commented:
yes but when finished do you rebind to clonetable?
0
 
taz8020Author Commented:
The two datagrids are bound to a binding source
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I don't use a BindingSource, so I'm not sure about that. I typically just create a connection to my database, then create a datatable and bind my DGV to that table.

What is the data source of your BindingSource?
0
 
taz8020Author Commented:
its a datatable fill from sql. so how would you rebind it

so when finished should i say mydatadrid.datasource = clonetable
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
That's all I do to "rebind" my datagridview:

MyDataGridView.Datasource = MyNewDatatable
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.