.NET General

Moderators: None (Apply to moderate this forum)
Number of threads: 749
Number of posts: 1301

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need Tips for Optimizing Windows Form Applications Posted by Isip2 on 13 May 2009 at 8:53 AM
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!
Report
Re: Need Tips for Optimizing Windows Form Applications Posted by Psightoplazm on 13 May 2009 at 12:32 PM
What is most likely happening is you are doing all of your data processing and/or database communications on the UI thread.
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:

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


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`~
Report
Re: Need Tips for Optimizing Windows Form Applications Posted by Isip2 on 13 May 2009 at 7:07 PM
Come to think of it, I do make tons of SQL queries to the database inside the Form constructor. I will try using the BackgroundWorker class for this, as you recommended.

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!
Report
Re: Need Tips for Optimizing Windows Form Applications Posted by Psightoplazm on 13 May 2009 at 11:25 PM
Well the issue most coders have when multi-threading their application is that the threads often trip over each other - or often have timing issues - or will just flat out get exceptions when trying to alter a UI element in some way that requires the UI thread.

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:
var myworker = new BackgroundWorker();
myworker.DoWork += delegate
{
    TextBox1.Text = "SampleText";
    TextBox1.Refresh();
}
myworker.RunWorkerAsync();


you should have received an error saying something about cross threading ui something or other

This is how this is solved:
var myworker = new BackgroundWorker();
myworker.DoWork += delegate
{
    TextBox1.Invoke(new MethodInvoker(delegate
    {
        TextBox1.Text = "SampleText";
    }));
}
myworker.RunWorkerAsync();


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`~
Report
Re: Need Tips for Optimizing Windows Form Applications Posted by Isip2 on 14 May 2009 at 12:12 AM
Very well explained.

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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.