473,372 Members | 991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

InvalidOperationException: object is currently in use elsewhere


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);
}
}
}
}

Dec 19 '05 #1
4 10330

Jos Lavrijsen wrote:
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. From the docs (my emphasis):
Brush Class

Thread Safety
Any public static members of this type are thread safe. **Any instance
members are not guaranteed to be thread safe.**
Looks like a dotnet bug to me.


Looks like behaviour as documented to me. Fonts aren't guaranteed
thread-safe either, so you've been lucky so far.

--
Larry Lard
Replies to group please

Dec 19 '05 #2
Jos Lavrijsen <Jo**********@discussions.microsoft.com> wrote:
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.


How can it be a bug when it's the documented behaviour, as Larry
pointed out earlier?

Do you really need to be doing UI operations on multiple threads in the
first place? Usually all UI operations should occur on a single thread.
Given the "Philips Medical Systems" bit, I guess you may be writing a
medical imaging application, in which case it's more likely that it's a
reasonable design, but it's worth checking.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 20 '05 #3

Ok, brushes may not be thread safe but it feels strange that an exception
is thrown even though i am not modifying the brush.
Apparently, a brush is not only a description but maybe also contains logic
to perform the actual drawing.

Drawing on bitmaps has nothing to do with UI operations.
In my use-case i am processing a huge amount of images in background to
be exported. To get a high throughput on multiprocessor systems, i use
multiple threads to do this processing.
I try to reuse brushes, fonts and pens to minimize the amount of garbage
produced.

Jos Lavrijsen.

"Jon Skeet [C# MVP]" wrote:
Jos Lavrijsen <Jo**********@discussions.microsoft.com> wrote:
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.


How can it be a bug when it's the documented behaviour, as Larry
pointed out earlier?

Do you really need to be doing UI operations on multiple threads in the
first place? Usually all UI operations should occur on a single thread.
Given the "Philips Medical Systems" bit, I guess you may be writing a
medical imaging application, in which case it's more likely that it's a
reasonable design, but it's worth checking.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 21 '05 #4
Jos Lavrijsen <Jo**********@discussions.microsoft.com> wrote:
Ok, brushes may not be thread safe but it feels strange that an exception
is thrown even though i am not modifying the brush.
Apparently, a brush is not only a description but maybe also contains logic
to perform the actual drawing.
I'd expect so, yes. Whatever the reason, it's a UI element.
Drawing on bitmaps has nothing to do with UI operations.
Well, it is in almost all uses, I'd say. Bitmaps are UI elements as far
as Windows is concerned, so you shouldn't access them (or brushes) on
the wrong thread.
In my use-case i am processing a huge amount of images in background to
be exported. To get a high throughput on multiprocessor systems, i use
multiple threads to do this processing.
I try to reuse brushes, fonts and pens to minimize the amount of garbage
produced.


I suggest you cache them by thread instead. You'll still save a lot of
churn, but without violating the WinForms threading principles.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: talljames | last post by:
Has anyone come across this exception before when dealing with C# graphics? Have come across some answers that point to the accelleration control under the screen's...
4
by: 6tc1 | last post by:
Hi all, I have just finished debugging a windows application and have solved the problem - however, I want to be sure that I understand the problem before I move on. Before I detail the problem,...
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
8
by: Sharon | last post by:
I have a DataGrid control on a Form. The data of the control is added by another application by Remoting. It means that a worker thread is invoking a UI thread. Therefore I used a delegate in the...
4
by: JJ | last post by:
When I run my form prog, if I maximise, then minimise (i..i. force the GUI to redraw) I get the following error: "The object is currently in use elsewhere" At this line: ...
1
by: chad | last post by:
system.InvalidOperationException: the object is currently in use elsewhere... has anyone experienced this error before? I am occasionally getting this error. I can't figure out what could...
3
by: boeledi | last post by:
Dear All, (First of all this is not a c# piece of code but it does not really matter). I would really appreciate if someone could help me. I am developing an ASP.NET web site and I have to...
4
by: =?Utf-8?B?Qm9uaQ==?= | last post by:
Hi, I got this problem. I'm implementing a pluggable winform program. My plugins are usercontrol and I load them in my program through a interface. Now if I close my application an error occurs:...
0
by: para15000 | last post by:
Hello I have a C# desktop application in which one thread that I create continously gets an image from a source(it's a digital camera actually) and puts it on a panel(panel.Image = img) in the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.