473,382 Members | 1,468 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,382 software developers and data experts.

InvalidOperationException: The object is currently in use elsewher

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 []

private void AddDefectUI(string xmlDefect)
// Doing the DataGrid data adding

I don't know if it's a thread problem, but sometimes I get the following

"System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Graphics.Dispose(Boolean disposing)
at System.Drawing.Graphics.Dispose()
at System.Windows.Forms.PaintEventArgs.Dispose(Boolea n disposing)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)"
Can anybody tell me what wrong?
Jan 4 '06 #1
8 3537
Hi Sharon,

Welcome to MSDN newsgroup!

Based on my understanding and the error message, the current problem is
caused when the Graphics object is being used by other handles it calls
the Dispose method.

At the current stage, please check whether you use the object in other
parts of your project. Additionally, I recommend you make a simple demo
which can repro the current problem and send it to me. ItĄŻll help me to
narrow down the current problem. Thanks for your understanding! My alias is
v-****@online.microsoft.com (remove .online).

IĄŻm looking forward your reply!


Yuan Ren [MSFT]
Microsoft Online Support

Jan 5 '06 #2
I can send you the code I'm using, but to summarize will take some time.

The info I have right now is that I'm not using multithreading that uses the
DataGrid control.
I'm not using GDI+ of the DataGrid control at all.

After taking a closer look at the exception, you can see that the invocation
was initiated by the Windows OS (NativeWindow.Callback). So maybe it open a
new thread that access the control graphics outside the UI thread.

To prevent that, I override the OnPaint() of the DataGrid and locked with
the lock statement.
Furthermore; at this OnPiant() I'm checking if the calling thread is the UI
thread or other, so it is other thread then the UI thread I'll know I I'll
post in here.

Any other idea is more then welcome.
Jan 10 '06 #3
Hi Sharon,

Thanks for your feedback.

First, this is not caused by Windows OS code, but .Net winform FCL code.
(NativeWindow.Callback). Based on the call stack, the exception is caused
by .Net Winform code when it is processing WM_PAINT message for the form.

I am not sure if your finding of override the OnPaint of DataGrid is the
root cause. Does it resolve your problem?

Currently, I can find a known bug regarding this call stack. Based on the
bug details, it seems that it is caused by ATI Rage Pro driver, and
updating the driver fixes the problem. You may give this a try.

If you still can not workaround this problem, I think a reproduce little
sample is helpful for us to understand this issue. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jan 11 '06 #4
Hi Jeffrey,

Thanks for your reply.

I don't really understand what driver you want me to install (the ATI Rage
Pro driver). Is it the graphics board device driver? it sound like it should
be something higher then that.

Actually, I have this problem in tree of our control:
(1) Native .NET GroupBox control.
(2) DataGrid that is inside my own UserControl.
(3) My own MAP control derived from the UserControl.

*The first one we don't care about because its form windows is hidden.
*The second one did not occur yet since I locked the OnPaint() by deriving
the DataGrid and locking the call to the bas.OnPaint().
* The third is still happening, although I locked its OnPaint(), but it's
because I'm using the GDI+ in some more places of the MAP control code other
then its OnPaint().

In order to confirm my suspicion that the .Net winform FCL code
(NativeWindow.Callback) is calling from a NON UI thread, I warped the
Application.Run(new MyForm()) in a try-catch, and also added handling for
unhandled exceptions
(System.Threading.Thread.GetDomain().UnhandledExce ption). in both the catch
exception I'm shown a MessageBox with the error. (This two handlers are at
the main form context).
Now the spoken exception was thrown again yesterday (Only in my MAP control)
BUT the two exception handling popped their MessageBoxes with the same
exception type and description AT THE SAME TIME (almost).

Therefore I'm quite sure that two threads was trying to use the GDI+ of the
control and one of them is not the UI thread. And as I mentioned before; I'm
not using any multi threading on the UI.
So my conclusion is that the .Net winform FCL code (NativeWindow.Callback)
do have a bug because it's invoking a UI call within a non UI thread.
Now I can prevent this error by locking all the code that uses any GDI+.

Maybe you have a better ideas?
Jan 11 '06 #5
Just to make sure:

Do I need to do Graphics.Dispose() / Image.Dispose() /
Graphics.ReleaseHdc(pDC) or should I let the garbage collector (GC) to do
that for me ?

Currently I'm disposing all the graphics objects that I'm using manually
(invoking the Dispose myself or using the using statement).
Jan 11 '06 #6
Some bad news...

The error was reproduce in our QA at the DataGrid itself although its
OnPaint() is protected by a lock.
Furthermore, the thrown exception was not caught nor by my try-catch around
the application Main function, nor by the unhandled exception catcher. It was
caught again by the CLR.

*** Important note: The QA PC has a dual Xeon dual core CPU, and this error
is only reproduced in that PC. But of course the QA PC does much more testing
then our development PC's.
Do you think it has anything to do with the dual CPU?

Does the .Net winform FCL code (NativeWindow.Callback) invoke the control
code from another thread?
Jan 12 '06 #7
More news:
I thinks it's a bad new because it tells me that I can not bypass the
problem at hand.

I disabled 3 CPU's so only single CPU will execute my program (I did it
using the Windows Task Manager), Yes, after only a few hours it still
happened. Same exception!

Note: The QA PC does not have dual CPU dual core as I said before. It has
dual XEON CPU's with hyperthreading. And it's shown on the Task Manager as 4
Anyone can help here please ?
Jan 15 '06 #8
Hi Sharon,

Sorry for late response!

At the current stage, this issue would be a product problem, if you need a
solution to work around this problem, it is better for you if one of our
PSS support engineers could work with you. This may need you submit a
support incident in Microsoft PSS (Product Support Service). As a MSDN
subscriber, you have two free support incidents.

By the way, if the problem is confirmed by Microsoft PSS as a product
issue, PSS won't charge money. Every MSDN subscriber has two free support
incidents. You could use one of them to contact Microsoft PSS. So you don't
need to pay money yet. Surely if the issue is a product issue, you still
have two free support incidents.

For your reference, I attached steps to contact Microsoft PSS here: You can
contact Microsoft Product Support directly to discuss additional support
options you may have available, by contacting us at 1-(800)936-5800 or by
choosing one of the options listed at



Yuan Ren [MSFT]

Microsoft Online Support

Jan 17 '06 #9

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

Similar topics

by: Mike Fiedler | last post by:
On a client web services request, I have a SoapExtension that is adding a header to the message in the BeforeSerialize stage. If the Web Service request is for an operation with...
by: Jos Lavrijsen | last post by:
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...
by: banduraj | last post by:
I get an System.InvalidOperationException when trying to Serialize a struct that has a type refrenced from another library. The exception is: System.InvalidOperationException was unhandled...
by: snowie | last post by:
I have a simillar problem to what others have had before me. I just can't find the solution in any earlier post here at the scripts (or any other forum). I am calling a web service that returns a...
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...
by: Joseph Geretz | last post by:
System.InvalidOperationException: WebServiceBindingAttribute is required on proxy classes. My environment: Visual Studio 2005, targeting FX 2.0; I've developed a Web Service which uses DIME to...
by: Spam Catcher | last post by:
Anyone know what this error means? The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was...
by: Jeroen | last post by:
We are experiencing a tuff-to-debug problem ever since we introduced a WebBrowser control into our failry large application. I'm not sure if the problem description below contains enough details,...
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...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
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,...
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...
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...
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
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.