Question

According to the usual Thread safety statement.

Any public static (Shared in Visual Basic) members of this type are thread safe

Which makes Brushes.White thread safe but not new SolidBrush(Color.White)

My first question is why, what are the technical differences in the back?

Second, can threadsafe simple brushes such as Brushes.White be created?

Was it helpful?

Solution

This is boilerplate verbiage for 99.9% of all .NET classes. Pretty useless. A brush has an internal immutable field, it is checked when you set the brush's Color property. Doesn't have anything to do with threading, it just prevents you from changing the color of a stock brush (like Brushes.White). Which would of course be pretty bad. With no other property left to modify, that makes a stock brush thread-safe by coincidence.

Odds are fairly decent that your own SolidBrush is thread-safe too, GDI+ has some internal locking built-in. You can for example have multiple threads draw to a bitmap, as long as they are different bitmaps. It is however not explicitly documented to be thread-safe so you'd do this at your own risk. Which is fairly pointless to exercise, it just doesn't make much sense to change the color of a brush after creating it. Or for that matter to have multiple threads use the same brush. A brush is very cheap, it takes only a microsecond to create one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top