Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 450483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:52:24+00:00 2026-05-12T21:52:24+00:00

Normally when I override the OnPaint method, I create pens and brushes, etc inside

  • 0

Normally when I override the OnPaint method, I create pens and brushes, etc inside it and then dispose them.

I also read somewhere that instead of recreating these pens and brushes, etc to create them once as static members, and then dispose them once when the form is closed, etc.

Is this a better practice?

Is there a better way for this?

I can assume that since OnPaint is called 1000s (?) of times, that would create a lot of work for the GC compared to creating them only once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:52:25+00:00Added an answer on May 12, 2026 at 9:52 pm

    If the brushes and pens don’t change, it’s certainly better to create them once and reuse them. Note, however, that if your control might be used on multiple threads (which is very unlikely), you should either make them ThreadStatic (and initialize on first use per thread) or make them instance members (and dispose them in your control’s Dispose override); otherwise, you’ll get unreproducable GDI+ errors, as GDI+ objects cannot be used on multiple threads at once. The same is true for images.

    If the they do change (for example, if you use gradient brushes which depend on the control’s size), you might still want to store them in instance fields, and recreate them when the control’s size (or whatever) changes.

    Note, by the way, that if you use normal colors, you can use the static Brushes and Pens classes, which contain static brushes and pens for all of .Net’s built-in colors, and SystemBrushes and SystemPens for system colors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

normally I place my translations etc in properties - e.g. message_de.properties etc. I'm facing
How can I override TinyMCE's default formatting for basic stuff like bold, underline, and
Normally clicking 'X' button at the top right corner will exit the application. I
Normally the easiest way to debug is using printf . What can I do
Normally, if I complete a form, the data will be sent to the server
I want that the WPF application starts only in certain conditions. I tried the
I have a layout with 4 separate chunks. They are: A nav panel with
I use WordPress for several websites. I want to customize one of the RSS
I just installed the whole shebang in order to develop android apps in Eclipse
I have a class called Cell: class Cell: def __init__(self, value, color, size): self._value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.