What could cause Double Buffering to kill my app?

大憨熊 提交于 2019-12-01 16:11:16

You aren't supposed to dispose the Graphics object loaned to you during the Paint event, and that's what your using block improperly does.

The symptom is that the next time the Paint event fires, you get the same Graphics object back, but it is no longer bound to an in-memory HDC, causing Graphics.GetHdc() to fail as seen in your stack trace.

  1. It's possible that it outlives a single Paint event (and this is very likely the case with double-buffering, although it's also possible with single-buffering if the CS_OWNDC window style is set).

  2. There can be more than one handler for the Paint event.

So, event handlers should not call Dispose on the Graphics objects or allow a using block to do so. Instead, the .NET framework cleans up resources as appropriate after Paint event handling is complete.

You should test this on another machine to see if it's just your computer or not. For the most part, this shouldn't happen as a result of double buffering, but check to see if you are disposing any elements you shouldn't be in the Paint event or doing anything in the code that would have issues if done twice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!