Thread Performance
-
Hi All, Im hoping someone can give me an idea on what I may be doing wrong (if anything). In my project I have a Combo Box which I populate from a SQL Database (using a Dataset and a for statement looping through each record). I have done it on form load and it works well however I decided to run it from a background thread so I become familiar with threads and for a few other reasons. Anyway, i have done it using a Thread but I seem to have taken a very large performance hit. Without a thread, populating the combo box (on Form_Load) it takes about 1 second. With the Thread it takes about 6 seconds. Im assuming since Im new with threads there is something obvious in my code that is wrong. Here is the relevant code in question:- //Create Delegates for the Threads to Access The Forms UI private delegate void UpdateComboBox(ComboBox cboName,string strItemToAdd); Code in the Form_load Event //Hide the Status Label lblStatus.Visible = false; //Start by Enabling the Loading Circle //This is an indicator that the Background Thread is still running //The Background thread is connecting to the database and populating the Combo Boxes lblLoadingFormData.Text = "Loading Data"; lblLoadingFormData.Visible = true; lcFormData.Active = true; //Create the Thread which will Update the Combo Boxes Thread thrPopulateComboBoxes = new Thread(this.PopulateComboBoxes); thrPopulateComboBoxes.Start(); Code to Populate the Combo Box private void PopulateComboBoxes() { //This Method will Populate the ComboBoxes on the Form //Customers ArrayList al = new ArrayList(); //Customers Combo Box al = myDatabaseFunctions.PopulateComboBox("SELECT Name FROM tblCustomers ORDER BY Name ASC", "Name", "Customers"); foreach (string s in al) { this.Invoke(new UpdateComboBox(this.AddItemToComboBox),new object[] {cboCustomer,s.ToString()}); } } I know the issue is not with the Arraylist as this is the same sort of code I am using in a non thread and the performance is fine Finally the code to add to Combo Box private void AddItemToComboBox(ComboBox cboName, string strItem) { cboName.Items.Add(strItem); } Can any1 offer some thoughts as to why Im taking such a performance hit? Am I doing something wrong with the threads? Thanks in advance, Daniel.
-
Hi All, Im hoping someone can give me an idea on what I may be doing wrong (if anything). In my project I have a Combo Box which I populate from a SQL Database (using a Dataset and a for statement looping through each record). I have done it on form load and it works well however I decided to run it from a background thread so I become familiar with threads and for a few other reasons. Anyway, i have done it using a Thread but I seem to have taken a very large performance hit. Without a thread, populating the combo box (on Form_Load) it takes about 1 second. With the Thread it takes about 6 seconds. Im assuming since Im new with threads there is something obvious in my code that is wrong. Here is the relevant code in question:- //Create Delegates for the Threads to Access The Forms UI private delegate void UpdateComboBox(ComboBox cboName,string strItemToAdd); Code in the Form_load Event //Hide the Status Label lblStatus.Visible = false; //Start by Enabling the Loading Circle //This is an indicator that the Background Thread is still running //The Background thread is connecting to the database and populating the Combo Boxes lblLoadingFormData.Text = "Loading Data"; lblLoadingFormData.Visible = true; lcFormData.Active = true; //Create the Thread which will Update the Combo Boxes Thread thrPopulateComboBoxes = new Thread(this.PopulateComboBoxes); thrPopulateComboBoxes.Start(); Code to Populate the Combo Box private void PopulateComboBoxes() { //This Method will Populate the ComboBoxes on the Form //Customers ArrayList al = new ArrayList(); //Customers Combo Box al = myDatabaseFunctions.PopulateComboBox("SELECT Name FROM tblCustomers ORDER BY Name ASC", "Name", "Customers"); foreach (string s in al) { this.Invoke(new UpdateComboBox(this.AddItemToComboBox),new object[] {cboCustomer,s.ToString()}); } } I know the issue is not with the Arraylist as this is the same sort of code I am using in a non thread and the performance is fine Finally the code to add to Combo Box private void AddItemToComboBox(ComboBox cboName, string strItem) { cboName.Items.Add(strItem); } Can any1 offer some thoughts as to why Im taking such a performance hit? Am I doing something wrong with the threads? Thanks in advance, Daniel.
When
Invoke
is called from a worker thread, it sends a windows message to the UI thread which then runs the delegate. You are doing this for each string in your results, which will be slow. You could run the db call on a worker thread and then callInvoke
once with the entireArrayList
to populate theComboBox
. Also, you should probably use a thread pool thread for this, either directly or using aBackgroundWorker
. Nick---------------------------------- Be excellent to each other :)
-
Hi All, Im hoping someone can give me an idea on what I may be doing wrong (if anything). In my project I have a Combo Box which I populate from a SQL Database (using a Dataset and a for statement looping through each record). I have done it on form load and it works well however I decided to run it from a background thread so I become familiar with threads and for a few other reasons. Anyway, i have done it using a Thread but I seem to have taken a very large performance hit. Without a thread, populating the combo box (on Form_Load) it takes about 1 second. With the Thread it takes about 6 seconds. Im assuming since Im new with threads there is something obvious in my code that is wrong. Here is the relevant code in question:- //Create Delegates for the Threads to Access The Forms UI private delegate void UpdateComboBox(ComboBox cboName,string strItemToAdd); Code in the Form_load Event //Hide the Status Label lblStatus.Visible = false; //Start by Enabling the Loading Circle //This is an indicator that the Background Thread is still running //The Background thread is connecting to the database and populating the Combo Boxes lblLoadingFormData.Text = "Loading Data"; lblLoadingFormData.Visible = true; lcFormData.Active = true; //Create the Thread which will Update the Combo Boxes Thread thrPopulateComboBoxes = new Thread(this.PopulateComboBoxes); thrPopulateComboBoxes.Start(); Code to Populate the Combo Box private void PopulateComboBoxes() { //This Method will Populate the ComboBoxes on the Form //Customers ArrayList al = new ArrayList(); //Customers Combo Box al = myDatabaseFunctions.PopulateComboBox("SELECT Name FROM tblCustomers ORDER BY Name ASC", "Name", "Customers"); foreach (string s in al) { this.Invoke(new UpdateComboBox(this.AddItemToComboBox),new object[] {cboCustomer,s.ToString()}); } } I know the issue is not with the Arraylist as this is the same sort of code I am using in a non thread and the performance is fine Finally the code to add to Combo Box private void AddItemToComboBox(ComboBox cboName, string strItem) { cboName.Items.Add(strItem); } Can any1 offer some thoughts as to why Im taking such a performance hit? Am I doing something wrong with the threads? Thanks in advance, Daniel.
Hi, I didn't look at your code as it was unformatted, lacking PRE tags. However I think you should read this[^]. :)
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!