Race Condition with GDI+

G

Guest

I'm writing a program that deals extensively with the printer. For the most
part my application runs fine, but occasionally I run into some Exceptions.
The most common exceptions I run into are NullReferenceException and
InvalidOperationException : The object is currently in use elsewhere.
Unfortunately this appears to be a race condition. These exceptions appear
to ocurr while disposing GDI+ objects, the Graphics object seems to be the
biggest culprit. Unfortunately all of these Exceptions ocurr in
System.Drawing.dll, as a result no source code is avaialable. These
Exceptions always ocurr while my program is printing. I would provide the
source code for this, but these Exceptions are blown during various portions
of my printing algorithm (approximately 3,000 lines), and exposing that would
put my job in jeapordy. I'm hoping that someone out there is familiar enough
with GDI+ to know why these Exceptions are being thrown. Any help would be
appreciated.

Here is the stack trace for my exceptions:

************** Exception Text **************
System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Graphics.Dispose(Boolean disposing)
at System.Drawing.Graphics.Dispose()
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument
document)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at Erudite.CheckManagerSystem.CheckRunPrinter.BeginPrinting()
at
CheckManager.ChequePrinting.New_GUI.PrintForm.PrintingDataLoaded(CheckRun
checkRun, DataLoadedEventArgs args)
at Erudite.CheckManagerSystem.CheckRun.OnDataLoaded(CHECK_HEADERDataTable
checkHeaders)
at Erudite.CheckManagerSystem.CheckRun.LoadData(CheckType checkType)
at
Erudite.CheckManagerSystem.CheckRun.LoadHeaderData(CheckRunEventHandler
beginLoadingDataCallback, CheckRunDataLoadedHandler dataLoadedCallback)
at CheckManager.ChequePrinting.New_GUI.PrintForm.ubtn_Print_Click(Object
sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


************** Exception Text **************
System.ArgumentException: Invalid parameter used.
at System.Drawing.Image.get_FrameDimensionsList()
at System.Drawing.ImageAnimator.CanAnimate(Image image)
at System.Drawing.ImageInfo..ctor(Image image)
at System.Drawing.ImageAnimator.Animate(Image image, EventHandler
onFrameChangedHandler)
at System.Windows.Forms.PictureBox.Animate(Boolean animate)
at System.Windows.Forms.PictureBox.Animate()
at System.Windows.Forms.PictureBox.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.OnParentVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)



See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Drawing.SafeNativeMethods.GdipDrawImageRectRectI(HandleRef
graphics, HandleRef image, Int32 dstx, Int32 dsty, Int32 dstwidth, Int32
dstheight, Int32 srcx, Int32 srcy, Int32 srcwidth, Int32 srcheight, Int32
srcunit, HandleRef imageAttributes, DrawImageAbort callback, HandleRef
callbackdata)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect,
Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit
srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr
callbackData)
at System.Drawing.Bitmap.MakeTransparent(Color transparentColor)
at System.Drawing.Bitmap.MakeTransparent()
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at System.Windows.Forms.ThreadContext.OnThreadException(Exception t)
at System.Windows.Forms.Control.WndProcException(Exception e)
at System.Windows.Forms.ControlNativeWindow.OnThreadException(Exception e)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at
CheckManager.ChequePrinting.New_GUI.Controls.CheckRunReport.mi_PrintPreview_Click(Object sender, EventArgs e)
at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
at System.Windows.Forms.MenuItemData.Execute()
at System.Windows.Forms.Command.Invoke()
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

System.InvalidOperationException: The object is currently in use elsewhere.
at System.Drawing.Graphics.Dispose(Boolean disposing)
at System.Drawing.Graphics.Dispose()
at
Infragistics.Win.DrawCache.DisposeNativeWindowMethods(NativeMemoryDCData&
data)
at Infragistics.Win.DrawCache.FreeState()
at Infragistics.Win.DrawCache.Dispose()
at Infragistics.Win.UIElement.DrawHelper(Graphics graphics, Rectangle
invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode,
Boolean clipText, Boolean forceDrawAsFocused)
at Infragistics.Win.UIElement.Draw(Graphics graphics, Rectangle
invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode)
at Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics,
Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode
alphaBlendMode, Size elementSize)
at Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics,
Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode
alphaBlendMode)
at Infragistics.Win.UltraWinGrid.UltraGridUIElement.Draw(Graphics
graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode
alphaBlendMode)
at Infragistics.Win.UltraControlBase.OnPaint(PaintEventArgs pe)
at Infragistics.Win.UltraWinGrid.UltraGrid.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,
Int16 layer, Boolean disposeEventArgs)
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(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
 
B

Bob Powell [MVP]

Are you using CreateGraphics somewhere??

See the GDI+ FAQ on how and where to get Graphics objects.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

No I am not using CreateGraphics. I'm using the Graphics object that is
passed in the PagePrintEventArgs parameter of the PrintPage event of
PrintDocument.
 
N

Nathan Neitzke

It seems to me that you are trying to call Dispose() on the Graphics object
that is passed as a parameter to your method. If this is true, I would
remove the Dispose() call. It looks like you are attempting to Dispose a
Graphics object that is still being used by your application - hence the in
use and null reference errors.

As far as the other one - the Animate() that is too hard to tell because I
can't see what you are passing into the function call.

Hope this helps.
 
G

Guest

That does help. I'm not disposing the Graphics object, but I am disposing a
lot of different GDI objects. I'm almost certain that some of these GDI
objects are system objects, ie System Brushes, and System Pens.
 
N

Nathan Neitzke

Oh,
Yes, definately - never Dispose System GDI objects. Only Dispose GDI
objects that you explicitly create.

Glad I was able to help.

Take care.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top