.NET - Cross-Thread Call on DataGridView - vb.net

I am new to multi-threading and I stumbled across an error which I was hoping someone could explain to me why was happening.
My initial program code did not have multi-threading and I was populating some charts with data from a datagridview. I was getting values like this:
Val = dgv.Rows(counter).Cells(columnName).Value
Since I was populating four charts using the same grid, I wanted to put this chart population process on multiple threads. I got an error when trying to update GUI elements (such as labels and a progress bar) and stumbled across this thread:
.NET Controls: Why aren't all calls thread-safe?
So my fix was to have the main thread update the GUI elements, and the backgroundworkers just report progress to the main thread.
However, I still getting a cross-thread error when trying to read the value using the function above. I'd like to know why this is the case.
I solved my problem using Invoke (which I believe is the correct way of doing it), ie. something like this:
dgv.Invoke(New ReadDGVCallBack(AddressOf ReadDGV), counter, "Name")
where "Name" is the column name.
However, I still am curious to know why I can't read a value from a datagridview from another thread. Since I am not modifying it, shouldn't it be fine if data is just being read? Could someone shed some light on this?
Thanks for all the help

Related

How do I show a “Loading . . . please wait” with gif image message in vb.net for a long loading form?

vb.net
I have a form(called Form1) that is very slow because it contain datagridview how load data from sql server.As a result the form takes a long time to loaded.
So I create a form_Wait and in the load event of Form1 I put
Form_wait.showdialog()
Load_datagridveiw1("Select * from table1")
Form_wait.close()
My problem is the Form_Wait will be appear but the gift inside it (rectangular progress bar) is not showen ?
I used VS 2015
You cannot meaningfully show and animate GIF in a same thread as your UI sits. You would have to use a BackGroundWorker, which is basically a separate thread. Then the animation will work smoothly and the UI thread will run fine too. There are plenty of BackGroundWorker tutorials on the web.
But a huge warning horn rings reading your words. If it is such a huge amount of data causing the slow loading, I would strongly suggest to reconsider how do you fetch the data. Introducing meaningful filters and pagination are basic ways to tackle this.

Any workarounds for using a RefEdit control inside a Frame control when designing a UserForm in VBA?

I tried to emulate the UserForm design in Data>DataAnalysis>Regression as below, but got very strange errors, usually Excel just crashes. After some googling, it seems the error is because I put RefEdit control inside the frame control. I then deleted the frames and everything went well.
But I have to say having frames makes the interface pretty clear, for instance, it hels separate my input and output options. I wonder how Excel skips this bug and if there is any workarounds in my case.

Exporting thousand lines from datagridview to excel using vb.net

Hi I'm currently having a datagridview with thousand (about 3000+) rows of record and 9 columns. I was using this method I found to export the datagridview to excel. There was no problem exporting hundred of rows but when it comes to thousand of rows, it hangs and it wouldn't respond to anything.
What problem could it be? and if there's any other way that is faster/better than this?
Thank you!
The code is being executed on the UI thread so of course your application would freeze while it's executing. If you want the UI to remain responsive then you'd have to execute the code on a secondary thread. The issue there is that the data is coming from a control, so that part at least must be executed on the UI thread.
I would suggest that one possibility is to use a BackgroundWorker and do the work in the DoWork event handler. You can set up a loop that calls ReportProgress, which raises the ProgressChanged event on the UI thread and allows you to get the data in pages, then write it out to your spreadsheet on the background thread. I'll follow up with an example.

Using MS access Database as data source with DataGridView Cannot Seem to Update or Refresh DataGridView using conventional methods

I have searched thoroughly for an answer to my question concerning a direct way to update a dataviewgrid with the call of a subroutine. but i had to luck, so im going to post the question here, with a little bit of background code and detail to help further your understanding on whats going on.
So i have 2 forms currently one being frmMain, this form contains three buttons
btnAddItem - brings up frmAdd
btnSearch - no code yet
BtnPrint - no code yet
frm main also has a dataviewgrid - currently named dvgPEBGrid
i have this bound to a access database with the datasource named PEBTableBindingSource
now on to frmAdd
i have some fields that eventually get updated to the database
i successfully do this but after the form closes i cannot get the data grid to update
i have tried a lot of things and what usually happens is my data grid just clears all data and i can not get the grid to return data until i close program and reset. any suggestions would be greatly appreciated.

How to call multiple subs with a BackgroundWorker

I have not yet used BackgroundWorker but believe I need to use these in my code.
My app does a lot of database work, running many SQL queries in sequence. My problem appears typical, that the main form becomes unresponsive.
I want to be able to display progress using a progressbar and a toolstripstatuslabel. I am doing this already, but without the BackgroundWorker.
My code is - unsurprisingly - divided into a several subs, which are called in sequence by the main form.
All the examples I have seen include just simple BackgroundWorker DoWork events. What is the correct way to deal with calling other code? Just call the modules as usual in the DoWork event? I do understand these must contain no user interface code.
Am using VB.Net 2010
Thanks!