Today I've caught curious exception in the code like below:
Just an "optimization" not to create a pen every time and keep it static for "caching".
An exception message was odd "Object is currently in use elsewhere".
The root cause was that this method was used in several threads simultaneously.
class Drawer { private static readonly Pen = new Pen(Color.White) { Width = 5 }; public Bitmap DrawEllipse(int width, int height) { var bitmap = new Bitmap(width, height); using (var graphics = Graphics.FromImage(bitmap)) { graphics.DrawEllipse(Pen, 0,0, width, height); } return bitmap; } }
Just an "optimization" not to create a pen every time and keep it static for "caching".
An exception message was odd "Object is currently in use elsewhere".
The root cause was that this method was used in several threads simultaneously.
No comments:
Post a Comment