Perceived Performance and Form.Paint

As a followup to my caution about exceptions in Form.Paint(), I wanted to illustrate why this technique is so effective. Let's say you had a form with this code:

Private IsFirstPaint As Boolean = True
Private Sub DoWork()
Cursor = Cursors.WaitCursor
StatusBar1.Text = "Loading..."
System.Threading.Thread.Sleep(2000)
For i As Integer = 0 To 99
ComboBox1.Items.Add("ComboBoxItem " & i)
System.Threading.Thread.Sleep(5)
Next
ComboBox1.SelectedIndex = 4
System.Threading.Thread.Sleep(2000)
For i As Integer = 0 To 99
ListBox1.Items.Add("ListBoxItem " & i)
System.Threading.Thread.Sleep(5)
Next
ListBox1.SelectedIndex = 4
StatusBar1.Text = "Ready."
Cursor = Cursors.Default
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
DoWork()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
If IsFirstPaint Then
IsFirstPaint = False
Application.DoEvents()
DoWork()
End If
End Sub

So either we're doing 5 seconds of work in Paint, or we're doing 5 seconds of work in form Load. Here's what it looks like when the work is done in Form.Load:

movie of Form Load doing work

And here's what it looks like when the work is done in Form.Paint:

movie of Form Paint doing work

The amount of time is the same in both cases, but guess which one users will tell you is "faster"? Perceived performance is more important than actual performance.

Sure, you can do a lot better job with threading, but I guarantee that'll take a lot more work than three lines of code! That's why I love the IsFirstPaint and DoEvents combo: maximum benefit for minimum effort.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments