Painting in WPF is done with brushes. There appears to be two ways to do this, and I'm wondering what the cost is.
The way that is generally described is the naïve approach:
This certainly looks like it is creating a new instance of a class. How long does that class live? What is the impact on memory?
Alternatively, since I might well be using a brush with a color of green in many different places if I am using it at all, then I could create one brush at form scope and use it in various places.
For the most part, this will be no big deal. The benefit of creating it in one place is pretty minimal if I will only be using that green brush in a few places. However, there is a time when I am creating a new brush every 100 ms, and the brushes might not be repeated...well, maybe not EVER, as it might cycle through every possible color at a rate of 10 colors per second.
In Windows.Forms, this wasn't a big deal, because all I was creating was a color structure, which had no memory implications. Creating a class just might.
So, the naïve approach for this would be to create a new brush for each new color. The better approach would be to use one brush at form scope and change the color property. This is the approach I took, I just don't know whether I was being reasonably cautious, or overly cautious.
Also, it seems like I could use one brush for all painting and change the color as needed. Is there a best practice here?
The way that is generally described is the naïve approach:
Code:
SomeControl.Background = New SolidColorBrush(Colors.Green)
Alternatively, since I might well be using a brush with a color of green in many different places if I am using it at all, then I could create one brush at form scope and use it in various places.
For the most part, this will be no big deal. The benefit of creating it in one place is pretty minimal if I will only be using that green brush in a few places. However, there is a time when I am creating a new brush every 100 ms, and the brushes might not be repeated...well, maybe not EVER, as it might cycle through every possible color at a rate of 10 colors per second.
In Windows.Forms, this wasn't a big deal, because all I was creating was a color structure, which had no memory implications. Creating a class just might.
So, the naïve approach for this would be to create a new brush for each new color. The better approach would be to use one brush at form scope and change the color property. This is the approach I took, I just don't know whether I was being reasonably cautious, or overly cautious.
Also, it seems like I could use one brush for all painting and change the color as needed. Is there a best practice here?