G
Guest
When i try to re-use brushes or pens over multiple threads, i often get
an InvalidOperationException with the message 'object is currently in use
elsewhere'.
I also tried this by re-using fonts but no problems there.
So, it looks like brushes and pens cannot be used in different threads
simultaneously.
Looks like a dotnet bug to me.
This exception is easily reproducible with the following code, both on single
and multi processor systems:
Jos Lavrijsen (Philips Medical Systems)
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Jos.Test.Problems {
/// <summary>
/// This test shows that brushes cannot be shared over multiple threads
/// Author: Jos Lavrijsen
/// Creation: 26 November 2005
/// </summary>
public class MTDrawProblem {
delegate void Delegate();
static Brush brush = new SolidBrush(Color.Blue);
public static void Start() {
for (int i = 0; i < 100; i++) {
new Delegate(TestIt).BeginInvoke(null, null);
}
}
private static void TestIt() {
try {
Bitmap bitmap = new Bitmap(1024, 1024,
PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(bitmap);
g.FillRectangle(brush, 0, 0, 1024, 1024);
} catch(InvalidOperationException ex) {
Console.Out.WriteLine(ex);
}
}
}
}
an InvalidOperationException with the message 'object is currently in use
elsewhere'.
I also tried this by re-using fonts but no problems there.
So, it looks like brushes and pens cannot be used in different threads
simultaneously.
Looks like a dotnet bug to me.
This exception is easily reproducible with the following code, both on single
and multi processor systems:
Jos Lavrijsen (Philips Medical Systems)
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Jos.Test.Problems {
/// <summary>
/// This test shows that brushes cannot be shared over multiple threads
/// Author: Jos Lavrijsen
/// Creation: 26 November 2005
/// </summary>
public class MTDrawProblem {
delegate void Delegate();
static Brush brush = new SolidBrush(Color.Blue);
public static void Start() {
for (int i = 0; i < 100; i++) {
new Delegate(TestIt).BeginInvoke(null, null);
}
}
private static void TestIt() {
try {
Bitmap bitmap = new Bitmap(1024, 1024,
PixelFormat.Format32bppPArgb);
Graphics g = Graphics.FromImage(bitmap);
g.FillRectangle(brush, 0, 0, 1024, 1024);
} catch(InvalidOperationException ex) {
Console.Out.WriteLine(ex);
}
}
}
}