Perceived Performance and Form.Paint

As a follow-up 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.

Jeff Atwood

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow, Discourse, and staygold.us. Disclaimer: I have no idea what I'm talking about. Let's be kind to each other. Find me https://infosec.exchange/@codinghorror

⏲️ Busy signing you up.

❗ Something's gone wrong. Please try again.

✅ Success! Check your inbox (and your spam folder, just in case).

Related posts

Recent Posts

If we do not stop to help each other, what do we become?

Yesterday, I received this email as a response to You Can't Vibe Code Love. It's such a remarkable and powerful statement that I asked permission to share it here, in its entirety, with personal information redacted: Hey Jeff, Hope you and your family are doing well.

By Jeff Atwood ·
Comments
You Can't "Vibe Code" Love

You Can't "Vibe Code" Love

About a year ago, I was offered a presentation slot at the WeAreDevelopers World Congress in Berlin. I rarely take speaking engagements, especially international ones, but this one arrived at just the right time, the right place, and with the right person – I said yes, on the contingency that Ben

By Jeff Atwood ·
Comments
An interactive TWiT Series You're the hero of the story! Choose from 1,024 possible endings. OFF by ONE with Jeff Atwood Hosted by Leo Laporte "every choice changes everything!"

Every Choice Changes Everything: The Show

About 3 weeks ago, Leo Laporte and I recorded the first episode of what will be a new monthly show on the TWiT network. Naming things is hard, and we almost voted on the name, like we did for Stack Overflow, but we quickly landed on Off By One with

By Jeff Atwood ·
Comments
Thank You For Being a Friend

Thank You For Being a Friend

It's been one of those months, and by that, I mean one of the 663 months since I was born. This won't be a long post, because I only have two things to say. First, I'm really glad we re-ordered the GMI (Guaranteed

By Jeff Atwood ·
Comments
I’m feeling unlucky... 🎲   See All Posts