When i try to re-use brushes or pens over multiple threads, i often get
an InvalidOperatio nException 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.Proble ms {
/// <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(Colo r.Blue);
public static void Start() {
for (int i = 0; i < 100; i++) {
new Delegate(TestIt ).BeginInvoke(n ull, null);
}
}
private static void TestIt() {
try {
Bitmap bitmap = new Bitmap(1024, 1024,
PixelFormat.For mat32bppPArgb);
Graphics g = Graphics.FromIm age(bitmap);
g.FillRectangle (brush, 0, 0, 1024, 1024);
} catch(InvalidOp erationExceptio n ex) {
Console.Out.Wri teLine(ex);
}
}
}
} 4 10366
Jos Lavrijsen wrote: When i try to re-use brushes or pens over multiple threads, i often get an InvalidOperatio nException 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
Jos Lavrijsen <Jo**********@d iscussions.micr osoft.com> wrote: When i try to re-use brushes or pens over multiple threads, i often get an InvalidOperatio nException 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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**********@d iscussions.micr osoft.com> wrote: When i try to re-use brushes or pens over multiple threads, i often get an InvalidOperatio nException 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Jos Lavrijsen <Jo**********@d iscussions.micr osoft.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
properties/settings/advanced/troubleshooting.
ie the graphics driver.
Other answers include disposing, revalidate.
Any suggestions would be appreciated.
|
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, this problem
requires some understanding of threading concepts.
Basically, the class contained both a PictureBox object as well as a
corresponding Image object (they both had a copy of the same picture).
The following was used to access the...
|
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 get the best performance for the utility class, its members, as well as
the System.Drawing.Graphics object, are static:
|
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 Form invocation like this:
public delegate void AddDefectEventHandler(string xmlDefect);
public void AddDefect(string xmlDefect)
{
this.Invoke(new AddDefectEventHandler(this.AddDefectUI), new object
{xmlDefect});
|
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:
e.Graphics.DrawString(MenuText.Substring(0, 1), underLinedFont, FontBrush,
textLayoutRectangle, sf)
| |
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 possibly cause it. When the prog runs
thru the loop, the error could be thrown anytime. Sometimes it doesn't at all.
Seems like it's telling me that the picturebox is in use at the same time
that it wants to be updated. But I timed it...
displayImg =...
|
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 deal with images
upload.
When the user is selecting an image, I save it as such together with a
thumbnail version.
|
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: "Object is currently in use
elsewhere". If I comment the line where a I use the Activator.CreateInstance
it doesn't happen.
I tried to disposed the object created but nothing but If I do a simple
new of my object without using reflection it...
|
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 GUI(which
must be another thread as it is the code-behind of a control.
The application works but on some machines I get the following error at random time
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |