Winform具有Red X异常 [英] Winform has Red X exception

查看:29
本文介绍了Winform具有Red X异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第3方控件(ComponentOne),间歇性地会得到这个典型的红色X框,如该典型图像所示.起初我以为我有GDI泄漏,因此在做完腿部工作之后,我验证了在绘制控件时我的GDI编号是可以的.

I am using a 3rd party control (ComponentOne) and at intermittently I would get this typical red X box such as this typical image shows. At first I thought I have a GDI leak, so after doing some leg works I verified my GDI numbers are fine when drawing controls.

在Google上搜索之后,我发现此类错误发生在OnPaint()事件上,因此,即使我在将控件调用到Render时进行了尝试捕获,也无法捕获该错误.

After Googling around I found that this kind of error happens on the OnPaint() event and therefore even if i put a try and catch when calling the control to Render, it wont catch it.

所以我的下一步是在我的代码中进行以下覆盖.

So my next step is to have the following override in my code.

        protected override void OnPaint(PaintEventArgs e)
       {
           try
           {
               base.OnPaint(e);
           }
           catch (Exception ex)
           {
               this.Invalidate();  //attempt to redraw the control
               XmSam.Log(ex);
           }
       }

我认为应该可以解决这个问题,但是我不能一直重现这个问题,因此我还不能完全测试上面的代码.我的问题是,如果我呈现控件并且它具有异常,那么此代码将尝试重画控件.这会陷入无限循环并冻结UI吗?还是您认为导致异常的任何原因都会消失,并且在第二个控件重绘时,它应该可以正常显示?

I think that should do the trick but I can't recreate this problem all the time and so I haven't been able to fully test the above code yet. My question is, if I render my control and it has exception then this code will attempt to redraw the control. Will this stuck in an indefinite loop and freezes my UI? or would you think whatever caused the exception will go away and upon the second control redraw and it should render fine?

推荐答案

我认为应该可以解决问题

I think that should do the trick

不,您使情况更糟.现在,您的OnPaint()方法将一遍又一遍地运行,可能会反复遇到相同的异常.您还会看到程序也烧掉了100%的内核.

No, you made it far worse. Now your OnPaint() method is running over and over again, probably falling over on the same exception repeatedly. You'll see your program burning 100% core as well.

在OnPaint()中获取异常并不是您真正可以生存的东西.对于用户而言,没有什么好看的,这是一个有保证的支持电话.不用隐藏问题,而是使用异常来找出实际出了什么问题并修复.使用Debug + Exceptions,选中CLR异常的Throwed复选框,以便在引发异常时调试器将停止.以防万一,在框架代码中引发异常的可能性不大,也可以使用工具+选项",调试"并取消选中启用我的代码"选项.

Getting an exception in OnPaint() is not something you can really survive. There's nothing to look at for the user, that's a guaranteed support call. Instead of hiding the problem, use the exception to figure out what actually went wrong and fix the problem. Use Debug + Exceptions, tick the Thrown checkbox for CLR exceptions so the debugger will stop when the exception is raised. Just in case, it is not unlikely that the exception is raised in framework code, also use Tools + Options, Debugging and untick the "Enable Just My Code" option.

这篇关于Winform具有Red X异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆