Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread Performance

Status
Not open for further replies.

danielkelly

IS-IT--Management
Aug 13, 2006
28
AU
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.
 
it's not the thread that is taking too much time, it's Invoke. it's using reflection to load each item.

normally you do your GUI work on the main thread. fetching and manipulating the data is done in the background, the results are passed back to the main thread and the items are added to the DDL. that's my understanding of it.

I wouldn't use raw threads. I would use a message service bus (nservicebus, rhino.bus, mass transit). this would handle the threading and asynchronous for me. my code deals with the business problem at hand, not the infrastructure to get it working.

check out Udi's blog he is an expert in the areas of enterprise applications, SOA, and message/asynchronous systems.

there are 2 ways to gets values across threads. One is domain context or something like that the other is Invoke as part of winform gui controls. Udi says the Invoke method is great for small updates, but if you are updating the GUI with large amounts of data the bound context is the way to go. I don't have much experience with either as I deal with web applications.

sorry this last paragraph is so vague. If any of this information comes back to me I'll post back.



Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
should have searched for forum first :) thread732-1549246 isn't your exact scenario, but deals with GUI updates on multiple threads. the very last post, may have some code will with help with Invoking GUI updates. still haven't found anything on the [Data/Domain/Bound]Context (it's grating me that I can't remember this:) )

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top