BufferedGraphicsContext错误
时间:2020-03-05 18:51:52 来源:igfitidea点击:
经过几个小时的应用程序使用后,我每天都在同一时间收到以下错误并调用堆栈。任何人都可以对正在发生的事情有所了解吗?
System.InvalidOperationException: BufferedGraphicsContext cannot be disposed of because a buffer operation is currently in progress. at System.Drawing.BufferedGraphicsContext.Dispose(Boolean disposing) at System.Drawing.BufferedGraphicsContext.Dispose() at System.Drawing.BufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle) at System.Drawing.BufferedGraphicsContext.Allocate(IntPtr targetDC, Rectangle targetRectangle) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.MenuStrip.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
解决方案
回答
我们是在黑暗中拍摄多幅画吗?如果我们要进行绘画相关的工作,请在GUI线程上进行,或者仔细同步代码。
回答
MSDN论坛上有一个很长的讨论此错误的地方。在大多数情况下,错误显然与以下任一情况相关:
- 一个潜在的OutOfMemory问题,表现为BufferedGraphicsContext异常,可能是由于框架错误所致。
- GDI对象泄漏(创建GDI对象而不处理它们)。
我回想起一年多前我自己遇到的这个错误,它肯定与内存问题有关,这使我们的应用程序长时间运行后会填满所有可用的VM,因此#1与我所观察到的一致。