Its taking time to load the form due to database operations and due to dynammic control additions
Act开发者_如何学Cually I am creating one application where 8 tab pages are there and each page containing about 10 comboboxes which i am adding at runtime. And also their items also fetching from excel file. And to select the item in these comboboxes i have to perform database operations. All this happends at Form_Load event. Due to this loading of form takes time. I have to increase the performance (i.e. reducing the load time). How i can i do this? Can i use mulltithreading ? But I am thinking that this may give exception like cross threading operation. Any suggestion.. Thanks in advance.
Can you save on load time by only populating the tabs as and when they are visible? You know when your user has selected a tab by handling the tab's SelectedIndexChanged
event.
You could use a BackgroundWorker.It will allow you to show your form before your data is loaded. Additionally, check this tips: Threading in Windows Forms.
If you start a background thread to load data which you then set to control, then you have to check for InvokeRequired
and then Invoke()
function again to get into the UI thread.
You'll also have to disable late-loaded UI elements until they're loaded. Also you'll probably have to delay saving of any data until all of your loadable elements are loaded (if you always end up setting all data from all tabs).
I would avoid preloading of any database data since it's so easy to start getting into very weird issues. Of course nHibernate cache is a different thing and can be used without much thought.
You could also prioritize more on which tab is actually showing and preload those things first. This could of course also hamper throughput since on multicore machine you could have more of the preloader threads, one touching DB, other excel and third doing whatever it does; all concurrently.
I would suggest that before you try any multithreading approaches, you try some caching or pre-loading. Is there a chance to load the relevant data from Excel or the database into memory before the Form is loaded, and reuse this data unless the original source changes? Of course, for this you need an update mechanism that detects when the Excel file or the database data might have changed and your data in memory got invalid.
I am upvoting hawbsl's answer; giving the impression of speediness is often just as useful as actually being quick in user interface work.
However, I can't help but mention one more thing. It seems like you might want to examine the code that is loading the data. Use a profiler to see where the time is spent. It's impossible to say for sure without knowing more about the code, but it doesn't seem like it should take very long to load that much data. For example, if you are using a .NET data provider to read the Excel data (through ODBC?) is the code getting a new connection for each combo box? Connection pooling typically should prevent that from being a problem (and I'm just making a wild guess at how the data is loaded anyway so it is likely not applicable).
精彩评论