C# 外部组件发生异常原因分析 [分析]

国内私募机构九鼎控股打造APP,来就送 20元现金领取地址: http://jdb.jiudingcapital.com/phone.html
内部邀请码: C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。 
------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

在项目中,用From2 启动 Report 正常,用From1 启动 Report 失败,日志:

2007-05-12 13:11:06
StartGenerateReportTask: System.Runtime.InteropServices.SEHException: 外部组件发生异常。
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at ZTE.CCG.Report.ReportController.StartGenerateReportTask(String contractHeader) in ***/report/quotationgenerationmodule/reportcontroller.cs:line 469

由于From2 和 Report 是同一个人开发的,直接启动 ReportController 是没有问题的,而我们自己通过相同的方法启动 ReportController 就出现了问题。开始以为是我们程序那里出现了问题,跟踪代码发现这个异常不固定,很难确定是那里出现了问题。
虽然显示的外部组建发生异常,可应用程序中没有调用任何其他的第三方应用程序哦。

对比Form1 和Form2 发现:
Form1:
        [STAThread]
        public static void Main(string[] args)
        {
           // lots of code...
           // it's same of the code posted on Form2.
        }

Form2:
        [STAThread]
        public static void Main(string[] args)
        {
                Application.EnableVisualStyles();
                Application.DoEvent();
               
           // lots of code...
        }
       
更改Form1中的代码继续执行,程序正常执行,没有任何错误。
取消 那两句话,错误又再次出现。

再查询了一下MSDN ,发现 :
“此方法启用应用程序的 Windows XP 可视化样式。如果控件和操作系统支持可视化样式,则控件将以这种样式进行绘制。若要使 EnableVisualStyles 生效,必须在应用程序中创建任何控件之前调用它;EnableVisualStyles 通常是 Main 函数的第一行。当调用 EnableVisualStyles 时,无需单独的清单即可启用可视化样式。
对于支持 FlatStyle 属性的控件,请确保将 FlatStyle 属性设置为 FlatStyle.System 值。”

搜索 FlatStyle ,发现在 Report 里面很多自定义控件这么写的:

this.xxxControlName.FlatStyle = System.Windows.Form.FlatStyle.System;

也就是说,控件是使用的系统的样式。在使用系统样式的时候必须告诉系统启用虚拟样式。
否则可能出现上述应用程序异常。

为了验证,这个猜想,在From1 中注释掉:
Application.EnableVisualStyles();
Application.DoEvent();
果然程序执行时回出现异常。

在google 上搜索了一下,发现很多上述类似异常的都或多或少提及到XP 样式了。

相关资源:
1).NET 1.1 Framework Fix:
http://support.microsoft.com/Default.aspx?kbid=899511
2)VS2005 中解决方法:
http://support.microsoft.com/kb/897298


Microsoft MSDN 论坛的一些相关信息:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=48878&SiteID=1
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=388724&SiteID=1

 

转载于:https://www.cnblogs.com/AloneSword/archive/2007/05/12/2237560.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值