Hi guys!
I have been doing minor projects in C# in the past, but this is the only time where I feel I need to optimize the application. Specifically, I am looking for ways to improve the performance of the application through the following:
1. Data caching before application starts.
2. Improving drawing performance of user controls.
Especially for no. 2, it is very noticeable that when the application starts, a slight delay in the drawing of controls and images is evident (My application has a lot of custom controls). You can easily see some controls being drawn first compared to others. I am pretty much sure this is not due to my hardware, since more complex applications run flawlessly. What could be a way to fix this?
If my statements are vague, forgive me, because I really have no clue on the solution and even the problem itself. If anyone can point me to the right direction, you would really be helping me out.
Thanks guys!
Comments
The best way to split this up is to start using BackgroundWorkers for all of your db/data processing.
Your application's UI actually all runs in its own special thread. All the code that you have in a UI object's handlers, constructors or whatever are all occuring on this thread and fighting for processing time. The more burden you put on this thread the harder it is for the UI to find time to draw itself etc...
The .net BackgroundWorker class works with .net thread pooling and allows (as far as I am aware) the most optimized way of background processing. It's also really easy to use:
[code]
var worker = new BackgroundWorker();
worker.DoWork += delegate
{
// All your processing code here...
}
worker.RunWorkerCompleted += delegate
{
// When the task is done this handler will fire
}
worker.RunWorkerAsync(); // Starts the worker
[/code]
Just remember when doing this to make sure that your code is cross-threading safe - and that you are sending UI requests back to the UI thread.
So for example - if after loading your data - you want to display it on a textbox somewhere you will need to call the "Invoke" method off of one of your UI elements.
><//~Psightoplasm`~
However, I would be displaying this data in a datagridview. Can you please explain this cross-threading safety and how to use the invoke method?
Thanks a lot!
In your case what is going to happen is your form is going to display before your data is ready to be displayed - causing a blank datagrid and eventually an exception when your background thread attempts to put the data in the view. What you might try doing is to display a loading window of some kind until the data is ready to be displayed - and then show the data view.
Now - the exception you may have seen if you've tried this can be solved by calling the "Invoke" method off of any UI element. The invoke method will add whatever task you give it to the UI execution queue on the UI thread.
To see what I'm talking about - put together a form and give it a textbox. Then try this out:
[code]
var myworker = new BackgroundWorker();
myworker.DoWork += delegate
{
TextBox1.Text = "SampleText";
TextBox1.Refresh();
}
myworker.RunWorkerAsync();
[/code]
you should have received an error saying something about cross threading ui something or other
This is how this is solved:
[code]
var myworker = new BackgroundWorker();
myworker.DoWork += delegate
{
TextBox1.Invoke(new MethodInvoker(delegate
{
TextBox1.Text = "SampleText";
}));
}
myworker.RunWorkerAsync();
[/code]
This will place the assignment statement on the UI thread and will wait for it to execute before proceeding.
Now keep in mind you want to do all of your processing outside of the UI thread so only put what you absolutely have to in the Invoke method and nothing more.
For more information about multithreading look into "Thread Locking", "deadlock", "livelock", and "threadsafe". These keywords will help illustrate what issues you might run into in a multi threaded environment.
><//~Psightoplasm`~
I have actually started multi-threading in my application for the processes after loading. I will try experimenting on this now.
Thanks Psightoplasm, you're a great help.