Reputation:
I'm new to vb.net and I have a question. While I'm loading a large amount of data to my data grid the programm frozen until all data are loaded.
My question is. It is possible that the program be unfrozen while data is being loaded into grid, and if so could you show me a simple example.
And finally my question is. Is this a good or bad idea and why?
Upvotes: 1
Views: 611
Reputation: 654
I think it is a good idea, in this modern era, I think you should use the utmost advantage of what technology has to offer, that said, let me first explain the solution to your problem and basics on which an Windows Forms Application, WPF or whatever you are trying to use works on. Your form is basically running on a thread that is referred to as the "Main Thread", as long as the main thread is busy doing a process, it can't process other events or tasks such as giving response to certain inputs, in simpler terminology, one might say it is "hanged" or "frozen". If you are dealing with big amounts of data or long time taking processing, the simplest method to avoid this problem is to move that certain task to another thread. For this, you can use System.Threading class or the BackgroundWorker. In threading, you can either use a ThreadPool or a thread if you want to. I'm going to teach you to how to use a thread. Firstly, type your hectic time taking code in a separate code block, either function/sub/etc.
Private Sub abc()
'code here
End Sub
Then declare a thread and address it to the function/sub/etc. That contains your time taking code.
Dim thr As New System.Threading.Thread(AddressOf abc)
Then you can set a few properties of that thread like its priority etc. And then you finally start it
thr.Start
But there's a little tricky part to this, when you are referring to objects or controls of one thread from another thread, you have to request it so you don't interfere with it while any other thread might also be using it. In your case, use the code below
If datagridview1.InvokeRequired = True Then
Invoke(sub() datagridview1.enabled = false) 'or whatever you want to do with it or assign any values to it
Else
datagridview1.enabled = false 'or whatever you want to do with it or assign any values to it
End If
Upvotes: -1
Reputation: 26454
You can use DataGridView.VirtualMode, it's specifically meant for showing large volumes of data.
Another way is to load to a data table first (could be in background thread), then assign to DGV.Datasource, as explained here. Be careful with this approach though, DataTable also becomes slow to populate after a certain amount of records (my ballpark estimate is >100K).
Upvotes: 2