Need Tips for Optimizing Windows Form Applications - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need Tips for Optimizing Windows Form Applications

Isip2Isip2 Posts: 16Member
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

  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    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:

    [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`~
  • Isip2Isip2 Posts: 16Member
    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!
  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    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:
    [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`~
  • Isip2Isip2 Posts: 16Member
    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.
Sign In or Register to comment.