tags:

views:

166

answers:

6

I've been writing a C# .NET application. i'm using WinForms. so, I've some forms that load really slowly especially due to the fact that they fetch some data from some XML files and display them in ListBox controls.

What I'm asking is : how do I make the forms load faster. or how do I proceed to show some image of rotating wheel (the one you usually see when waiting for an action to be completed by a software).

Thanks.

+6  A: 

When you're adding many items to a ListBox, make sure you call

lst.BeginUpdate();

before you begin adding items, and

lst.EndUpdate();

after all the items have been added.

Patrick McDonald
You might want to expand your comment to include 'why'.
j0rd4n
MSDN link: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.beginupdate(VS.80).aspxTip from that link: Use AddRange instead of calling Add multiple times, and you won't need to call BeginUpdate/EndUpdate.
Meta-Knight
+10  A: 

Look into the BackgroundWorker class it allows you to start a task off in a background thread (in your case reading an XML file) and trigger a callback on form when the task is complete. This leaves your UI thread free to perform useful actions (register a cancel click) or show a loading graphic.

The background worker also supports the possibility of implementing a progress callback so that your UI can inform the user of how much longer they need to wait.

A quick Google search pops up this article which appears to cover the basics.

Martin Harris
+1  A: 
  • Put the long running code in a BackgroundWorker to offload the work into a separate thread and keep the UI responsive.
  • Invoke the BackgroundWorker from the Shown event of the form (not the constructor).
  • Optionally handle the ProgressChanged event of the BackgroundWorker to update the UI progressively as you load data.
  • Set UseWaitCursor = true, and Enabled = false after starting the BackgroundWorker, and revert them in the Complete event of the BackgroundWorker.
Jon Grant
Damn - beat me by 30 seconds :)
Fry
A: 

Also, you can just change the cursor to a wait cursor at the beginning of init and back to standard at the end to let the user know something is happening, but it's better to use threading so that the (main form?) doesn't lock up. Nothing Says "I froze" like a locked up main screen. If it is a main form, maybe a splash screen that says "Loading..." or something to that.

Fry
+4  A: 

To show the rotating wheel, if you're using System.Windows.Forms, look at the Cursor class.

For example I made a class like this:

class WaitCursor : IDisposable
{
    Cursor m_previous;

    internal WaitCursor()
    {
        m_previous = Cursor.Current;
        Cursor.Current = Cursors.WaitCursor;
    }

    #region IDisposable Members

    public void Dispose()
    {
        Cursor.Current = m_previous;
    }

    #endregion
}

which I use like this:

    using (WaitCursor waitCursor = new WaitCursor())
    {
        //... any statements here, which take a long time ...
    }
ChrisW
A: 

If you are using XmlSerializer you should know that since a custom XmlSerializer for each type is created and compiled at run time it can cause a performance issue (although it's created and compiled once for each type as long as your application is running)

Beatles1692