Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Thread Performance

Thread Performance

Scheduled Pinned Locked Moved C#
databasequestiondesignperformancehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3678645
    wrote on last edited by
    #1

    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.

    N L 2 Replies Last reply
    0
    • U User 3678645

      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.

      N Offline
      N Offline
      Nicholas Butler
      wrote on last edited by
      #2

      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 call Invoke once with the entire ArrayList to populate the ComboBox. Also, you should probably use a thread pool thread for this, either directly or using a BackgroundWorker. Nick

      ---------------------------------- Be excellent to each other :)

      1 Reply Last reply
      0
      • U User 3678645

        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.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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!


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups