-
Aug 15th, 2014, 08:02 PM
#1
Thread Starter
Hyperactive Member
Speed up the form load
I have one form with several comboboxes. Each combo list contains about 10000 items. Each combo is populated by its own SQL Server stored procedure.
The load of the form takes about 6-8 seconds.
Text parts of the combos are populated by a separate SP. If I exclude the population of the combo lists then the form loads within a portion of a second.
I tried to populate combos after loading form on click events. On box is populated, then the focus stays on the clicked combo.
May I increase the speed by using the second thread somehow?
If yes then if I use a separate thread for each combo, will I get the hihger speed? Or the speed doesn't depend on the number of threads?
Thank you
-
Aug 15th, 2014, 10:25 PM
#2
Re: Speed up the form load
Here's one example of what you could do with multiple secondary threads:
vb.net Code:
Imports System.ComponentModel Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.ComboBox1.Items.Add("Loading...") Me.ComboBox2.Items.Add("Loading...") Me.BackgroundWorker1.RunWorkerAsync(New ComboBoxBindingData With {.ComboBox = Me.ComboBox1, .Query = "SELECT * FROM Table1", .DisplayMember = "Table1Name", .ValueMember = "Table1Id"}) Me.BackgroundWorker2.RunWorkerAsync(New ComboBoxBindingData With {.ComboBox = Me.ComboBox2, .Query = "SELECT * FROM Table2", .DisplayMember = "Table2Name", .ValueMember = "Table2Id"}) End Sub Private Sub BackgroundWorkers_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork, BackgroundWorker2.DoWork Dim bindingData = DirectCast(e.Argument, ComboBoxBindingData) Dim table As New DataTable Using adapter As New SqlDataAdapter(bindingData.Query, "connection string here") adapter.Fill(table) End Using bindingData.Table = table e.Result = bindingData End Sub Private Sub BackgroundWorkers_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted, BackgroundWorker2.RunWorkerCompleted Dim bindingData = DirectCast(e.Result, ComboBoxBindingData) With bindingData.ComboBox .DisplayMember = bindingData.DisplayMember .ValueMember = bindingData.ValueMember .DataSource = bindingData.Table End With End Sub End Class Public Class ComboBoxBindingData Public Property ComboBox() As ComboBox Public Property Query() As String Public Property DisplayMember() As String Public Property ValueMember() As String Public Property Table() As DataTable End Class
-
Aug 15th, 2014, 10:50 PM
#3
Re: Speed up the form load
What's the point of a combo box with 10000 entries? Nobody can search through them. I would guess that you are intending to use autocomplete, or some such, to narrow the choices down, but since it is costing you, perhaps you could come up with a different search mechanism?
Otherwise, is this your startup form? If it isn't, then start the loading (in a background thread) when the startup form starts.
My usual boring signature: Nothing
-
Aug 15th, 2014, 10:53 PM
#4
Re: Speed up the form load
you would never load multiple controls with 1 k items. That said 1k items would take less then 1 second. Trouble is we cant read minds. You did not bother to post your code.
-
Aug 16th, 2014, 07:33 AM
#5
Thread Starter
Hyperactive Member
Re: Speed up the form load
Thank you very much. The code provided by jmcilhinney id perfect. Comboboxes are populated one by one. Is there any way to stop appearing the value in the fastest combo (with less number of records) before the slowest combo is done? In other words, to prepare all datatables and only when all of them are ready to display show all values at once. Before that to keep waiting cursor.
Why do I use so unefficient approach? The application was started about 20 years ago when the biggest list for combo was about 30-40 records. The application was started in either VB2 or VB3 or something existing at that time. The current application in production is in VB6. I'm starting an upgrade to VB.Net and have almost no experience with this tool. I'm learning.
Thank you
-
Aug 16th, 2014, 08:35 AM
#6
Re: Speed up the form load
Originally Posted by chapran
Thank you very much. The code provided by jmcilhinney id perfect. Comboboxes are populated one by one. Is there any way to stop appearing the value in the fastest combo (with less number of records) before the slowest combo is done? In other words, to prepare all datatables and only when all of them are ready to display show all values at once. Before that to keep waiting cursor.
In the RunWorkerCompleted event handler, put the result data into a collection and then check whether the collection is full, i.e. contains the number of items for which you started background tasks. If it's full, populate all the ComboBoxes.
-
Aug 16th, 2014, 09:17 AM
#7
Thread Starter
Hyperactive Member
Re: Speed up the form load
Thank you very much. But with my very weak experience I doubt I will be able to write the code.
-
Aug 16th, 2014, 09:46 AM
#8
Re: Speed up the form load
Originally Posted by chapran
Thank you very much. But with my very weak experience I doubt I will be able to write the code.
That doesn't mean you can't try. If it doesn't work then you can post back here and show us what you've done and tell us what happens. We're here to help but that doesn't mean write all your code for you... not to me anyway. The best way to learn is to do. Trying and failing is all part of the learning process and, I would assume, you do want to learn.
-
Aug 16th, 2014, 09:58 AM
#9
Thread Starter
Hyperactive Member
Re: Speed up the form load
Sorry for my previous reply.
Thank you very much.
-
Aug 16th, 2014, 10:55 PM
#10
Re: Speed up the form load
Originally Posted by chapran
Sorry for my previous reply.
Thank you very much.
No problem. Like I said, if you can't get it to work, just be sure to post what you've done and explain what actually happens. We can then provide further assistance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|