RE: .net issue with doublebuffered set to true
- From: jetan@xxxxxxxxxxxxxxxxxxxx ("Jeffrey Tan[MSFT]")
- Date: Wed, 22 Nov 2006 03:09:22 GMT
Hi Chaz,
Michael's reply has revealed the reason of your problem. I will provide
more details information to prove this:
In .Net2.0, by setting a breakpoint in Form.OnPaint method, you will get
the call stack below:
DataGridViewTest.Form1.OnPaint C#
System.Windows.Forms.Control.PaintWithErrorHandling C#
System.Windows.Forms.Control.WmPaint C#System.Windows.Forms.Control.WndProc C#
System.Windows.Forms.ScrollableControl.WndProc C#
System.Windows.Forms.ContainerControl.WndProc C#
System.Windows.Forms.Form.WndProc C#
System.Windows.Forms.Control.ControlNativeWindow.OnMessage C#
System.Windows.Forms.Control.ControlNativeWindow.WndProc C#
System.Windows.Forms.NativeWindow.DebuggableCallback C#
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsaf
eNativeMethods.IMsoComponentManager.FPushMessageLoop C#
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner C#
System.Windows.Forms.Application.ThreadContext.RunMessageLoop C#
System.Windows.Forms.Application.Run C#
DataGridViewTest.Program.Main C#
As you can see the Control.WmPaint and Control.PaintWithErrorHandling
finally calls Form.OnPaint() method. Most of the magic happens in the
Control.WmPaint() method. If you use Reflector to examine the
Control.WmPaint source code, you will see that there are 2 code paths:
DoubleBuffered disabled and enabled.
#1, DoubleBuffered disabled. The key code snippet is listed below
PaintEventArgs args2 = new PaintEventArgs(ptr4, new
Rectangle(paintstruct2.rcPaint_left, paintstruct2.rcPaint_top,
paintstruct2.rcPaint_right - paintstruct2.rcPaint_left,
paintstruct2.rcPaint_bottom - paintstruct2.rcPaint_top));
try
{
if (this.GetStyle(ControlStyles.AllPaintingInWmPaint))
{
this.PaintWithErrorHandling(args2, 1, false);
args2.ResetGraphics();
}
this.PaintWithErrorHandling(args2, 2, false);
return;
}
finally
{
args2.Dispose();
if (!this.IsDisposed && this.IsHandleCreated)
{
ptr3 = this.Handle;
}
UnsafeNativeMethods.EndPaint(new HandleRef(this, ptr3), ref
paintstruct2);
}
return;
As you can see, after calling PaintWithErrorHandling to OnPaint method,
WmPaint method will dispose the entire PaintEventArgs object in "finally"
clause. So your explicitly dispose calling will not cause any problem.
#2, DoubleBuffered enabled. The key code snippet is listed below:
using (BufferedGraphics graphics1 = this.BufferContext.Allocate(ptr1,
rectangle2))
{
Graphics graphics2 = graphics1.Graphics;
graphics2.SetClip(rectangle1);
GraphicsState state1 = graphics2.Save();
using (PaintEventArgs args1 = new PaintEventArgs(graphics2,
rectangle1))
{
this.PaintWithErrorHandling(args1, 1, false);
graphics2.Restore(state1);
this.PaintWithErrorHandling(args1, 2, false);
graphics1.Render();
}
}
Note: after calling PaintWithErrorHandling, graphics1.Render() method
internally still needs to use the Graphics object cached in it to render
the graphic to the DC. So, if you have explicitly disposed the Graphics
object, there will cause problem sometime later.
Anyway, I assume this is the root cause of your problem based on the
current information combined with source code. If you do not think so,
you'd better provide the detailed stack trace of ArgumentException. It will
reveal a lot of important information of the exception.
Thanks.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- Prev by Date: Re: .net issue with doublebuffered set to true
- Next by Date: Re: Multiple GDI newsgroups
- Previous by thread: Re: .net issue with doublebuffered set to true
- Next by thread: Re: .net issue with doublebuffered set to true
- Index(es):
Relevant Pages
|