RE: .net issue with doublebuffered set to true

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

.



Relevant Pages

  • Re: Drawing String on Path?
    ... do you mean by "draw a string on a graphics path"? ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Microsoft Asleep At The Wheel Again: Missing the New Era of CAD
    ... I just learned Microsoft acquired Vexcel and Massive ... injection technology to inject an ad into the wallpape which is pasted to ... 3D graphics programming has been around for more than 20 ... platforms that natively support 3D vector graphics. ...
    (microsoft.public.dotnet.general)
  • Re: My Migrations: PC architecture
    ... Windows 1 supported Hercules graphics as did many games (certainly ... and one for the Hercules HGC." ... The languages 'BASIC' and 'C' do not 'support' graphics at all. ... IBM salesmen like to talk about raw hardware speed. ...
    (comp.lang.cobol)
  • Re: /boot/loader graphics support & extensibility
    ... Support for i386/amd64 machines that have VGA hard- ... but do they really have VGA-compatible hardware? ... I played with CG6 graphics hardware on 32bit ...
    (freebsd-hackers)
  • Summary: Resetting VGA Resolution
    ... has a PowerStorm 4D50T graphics card. ... Some of the PWS's had graphics cards where you had to set the ... resolution too high for the monitor or the card or you could damage the ... basic VGA support and it sounds like no X server is starting up. ...
    (Tru64-UNIX-Managers)