当前位置: 首页 > 编程语言 > C#

什么会导致双缓冲终止我的应用程序?Share

时间:2023-04-11 03:08:57 C#

是什么导致双缓冲终止我的应用程序?我有一些使用GDI+绘制到屏幕上的自定义(winforms)组件。为了防止重绘闪烁,我决定启用双缓冲,所以我在构造函数中添加了一行:publicColourWheel(){InitializeComponent();this.DoubleBuffered=true;}适用于此组件(ColourWheel)。当我将同一行添加到我的其他两个(结构相似的)组件的构造函数时,我得到一些奇怪的症状:当我尝试运行组件打开的表单时,我在Application.Run(newForm());在Application.Run(newForm())上出现参数异常;.如果我切换到设计模式,我会收到有关组件的错误消息,该组件的参数具有未处理的异常。我是否对它们中的一个或所有进行双重缓冲似乎并不重要,它仍然适用于ColourWheel但不适用于其他。作为记录,我还尝试了其他一些双缓冲技术。什么会导致双缓冲在一个组件上起作用而在另一个组件上不起作用?编辑:以下是运行时症状的异常详细信息:System.ArgumentException未处理Message=Parameterisnotvalid。Source=System.DrawingStackTrace:System.Drawing.Graphics.GetHdc(),System.Drawing.BufferedGraphics.RenderInternal(HandleRefrefTargetDC,BufferedGraphicsbufferatSystem.Windows.Forms.Control'sSystem.Drawing.BufferedGraphics.Render())中间。系统中System.Windows.Forms.UserControl.WndProc(Message&m)的System.Windows.Forms.ScrollableControl.WndProc(Message&m)处的System.Windows.Forms.Control.WndProc(Message&m)处的.WmPaint(Message&m).Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtrhWnd,Int32msg,IntPtrwparam,IntPtrlparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)位于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtrdwComponentID,Int32reason,Int32pvLoopData)atatSystem.Windows。Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文),位于TestForm.Program.Main()的System.Windows.Forms.Application.Run(FormmainForm)的System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int32原因,ApplicationContextcontext)在D:DocumentsandSettingsTomWrightMyDocumentsVisualStudio2010ProjectsColourPickerTestFormProgram.cs:System.AppDomain.ExExcuteAssembly(RuntimeAssemblyassembly,String[]args),line18atSystem.AppDomain.ExecuteAssembly(StringassemblyFile,EvidenceassemblySecurity,String[]args)Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()(ExecutionContextexecutionContext,ContextCallbackcallback)在System.Threading.ExecutionContext.Run的System.Threading.ThreadHelper.ThreadStart_Context(Objectstate)System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallbackcallback,objectstate),objectstate,booleanignoreSyncCtx)on.Threading.ThreadHelper.ThreadStart()InnerException:编辑2:导致问题的两个组件之一(更复杂)OnPaint处理程序:privatevoidValueSlider_Paint(objectsender,PaintEventArgse){using(Graphicsg=e.Graphics){g.DrawImage(this.gradientImage,newRectangle(0,0,paintArea.Width,paintArea.高度));如果(这个。showmarker){ColourHandler.HSValt=ColourHandler.RGBtoHSV(newColourHandler.RGB(this.SelectedColour.R,this.SelectedColour.G,this.SelectedColour.B));alt.Saturation=0;alt.value=255-alt.value;使用(Penpen=newPen(ColourHandler.HSVtoColour(alt))){pen.Width=(float)MARKERWIDTH;g.DrawRectangle(pen,0-pen.Width,this.brightnessPoint.Y-MARKERWIDTH,this.paintArea.Width+(pen.Width*2),MARKERWIDTH*2);在Paint事件期间,你不应该处理借给你的Graphics对象,这是你的using块做错的症状是下次触发Paint事件时,返回相同的Graphics对象,但它不再绑定到内存中的HDC,导致Graphics.GetHdc()失败,如堆栈跟踪所示。它可能需要多个Paint事件(这很可能是双缓冲的情况,但如果设置了CS_OWNDC窗口样式,则单缓冲也是可能的)。Paint事件可以有多个处理程序。因此,事件处理程序不应调用Graphics对象上的Dispose或允许使用块来执行此操作。相反,.NETFramework在Paint事件处理完成后根据需要清理资源。您应该在另一台计算机上测试它,看看它是否只是您的计算机。在大多数情况下,这不应该是双缓冲的结果,但请检查您是否正在处理任何不应该出现在Paint事件中的元素,或者是否在您的代码中执行任何操作,如果您执行两次,您将拥有问题。以上是C#学习教程:Whatcouldcausedoublebufferingtokillmyapplication?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: