Question:
Vb.net: Help with application.doevents/threading populating combobox?
raisin_smiles
2009-08-21 06:21:25 UTC
I had issue updating a combobox with 30K items, whilst this activity was running nothing else could take place and the form was not repainted.

I'm quite the newbie and read about background workers and implemented one for the combobox but soon realised that the actual update of the combobox could not be done in the background worker only when it had completed as nothing done in the backgroundwork could manipulate controls on the form.

I then had the idea to launch another form on top with a progress bar (Which reads a textfile line by line and utilises the Application.DoEvents in the loop which reads the data into a string array), populating the combobox in the parent form from the child form, disabling the main form.

Then once the data had been loaded, close the form and re-enable the main form.

This worked well but as the form contains tabcontrol when the tabpage is accessed which contains the combobox there is a 2-3 second delay for the first time only.

What solutions are there to handle such an event of populating combobox with 30K items whilst allowing the user to still interact with the main form?

I'm currently reading more about threading, easier threading maintenance with the backgroundworker and application.DoEvents.

Thanks
Rob
Three answers:
Asad Siddiqi
2009-08-21 06:34:22 UTC
Yes, you can update the UI from a separate thread. The separate thread however cannot access the combobox control was not created on this thread so to access it you will have to invole the control from the Initial thread using a delegate.



Here is an alternative work around:

Simply put this in your constructor



// CheckForIllegalCrossThreadCalls shud be set to false

System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;



Now you should be able to access the control from background worker thread. Some detail on the other workaround "Invoke" can be found on my blog:



http://asadsiddiqi.wordpress.com/2007/12/24/responsive-user-interfaces-with-ui-threads/
bittu
2016-12-13 13:00:35 UTC
Application.doevents Vb.net
2009-08-21 06:30:40 UTC
No combo box should ever have 30 thousand items in it.



What are you trying to do ? (serious question - not a wind up)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...