【转】CLR20R3 程序终止的几种解决方案
这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理异常(例如,线程池线程中的未处理异常)提供支撑,而 Framework 2.0 版中,公共语言运行库允许线程中的多数未处理异常自然继续。在多数情况下,这意味着未处理异常会导致应用程序终止。
一、C/S 解决方案(以下任何一种方法)
1. 在应用程序配置文件中,添加如下内容:
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
</configuration>
2. 在应用程序配置文件中,添加如下内容:
<configuration>
<startup>
<supportedRuntime version="v1.1.4322"/>
</startup>
</configuration>
3. 使用Application.ThreadException事件在异常导致程序退出前截获异常。示例如下:
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.Run(new ErrorHandlerForm());
}
// 在主线程中产生异常
private void button1_Click(object sender, System.EventArgs e)
{
throw new ArgumentException("The parameter was invalid");
}
// 创建产生异常的线程
private void button2_Click(object sender, System.EventArgs e)
{
ThreadStart newThreadStart = new ThreadStart(newThread_Execute);
newThread = new Thread(newThreadStart);
newThread.Start();
}
// 产生异常的方法
void newThread_Execute()
{
throw new Exception("The method or operation is not implemented.");
}
private static void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception);
}
catch
{
try
{
MessageBox.Show("Fatal Windows Forms Error",
"Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
if (result == DialogResult.Abort)
Application.Exit();
}
// 由于 UnhandledException 无法阻止应用程序终止,因而此示例只是在终止前将错误记录在应用程序事件日志中。
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
Exception ex = (Exception)e.ExceptionObject;
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:/n/n";
if (!EventLog.SourceExists("ThreadException"))
{
EventLog.CreateEventSource("ThreadException", "Application");
}
EventLog myLog = new EventLog();
myLog.Source = "ThreadException";
myLog.WriteEntry(errorMsg + ex.Message + "/n/nStack Trace:/n" + ex.StackTrace);
}
catch (Exception exc)
{
try
{
MessageBox.Show("Fatal Non-UI Error",
"Fatal Non-UI Error. Could not write the error to the event log. Reason: "
+ exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
}
private static DialogResult ShowThreadExceptionDialog(string title, Exception e)
{
string errorMsg = "An application error occurred. Please contact the adminstrator " +
"with the following information:/n/n";
errorMsg = errorMsg + e.Message + "/n/nStack Trace:/n" + e.StackTrace;
return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Stop);
}
二、B/S 解决方案(以下任何一种方法)
1. 在IE目录(C:/Program Files/Internet Explorer)下建立iexplore.exe.config文件,内容如下:
<?xml version="1.0"?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true" />
</runtime>
</configuration>
2. 不建议使用此方法,这将导致使用 framework 1.1 以后版本的程序在IE中报错。
建立同上的配置文件,但内容如下:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v1.1.4322"/>
</startup>
</configuration>
3. 这个比较繁琐,分为三步:
⑴. 将下面的代码保存成文件,文件名为UnhandledExceptionModule.cs,路径是C:/Program Files/Microsoft Visual Studio 8/VC/
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Web;
namespace WebMonitor {
public class UnhandledExceptionModule: IHttpModule {
static int _unhandledExceptionCount = 0;
static string _sourceName = null;
static object _initLock = new object();
static bool _initialized = false;
public void Init(HttpApplication app) {
// Do this one time for each AppDomain.
if (!_initialized) {
lock (_initLock) {
if (!_initialized) {
string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");
if (!File.Exists(webenginePath)) {
throw new Exception(String.Format(CultureInfo.InvariantCulture,
"Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.",
webenginePath));
}
FileVersionInfo ver = FileVersionInfo.GetVersionInfo(webenginePath);
_sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",
ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);
if (!EventLog.SourceExists(_sourceName)) {
throw new Exception(String.Format(CultureInfo.InvariantCulture,
"There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",
_sourceName));
}
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
_initialized = true;
}
}
}
}
public void Dispose() {
}
void OnUnhandledException(object o, UnhandledExceptionEventArgs e) {
// Let this occur one time for each AppDomain.
if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)
return;
StringBuilder message = new StringBuilder("/r/n/r/nUnhandledException logged by UnhandledExceptionModule.dll:/r/n/r/nappId=");
string appId = (string) AppDomain.CurrentDomain.GetData(".appId");
if (appId != null) {
message.Append(appId);
}
Exception currentException = null;
for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException = currentException.InnerException) {
message.AppendFormat("/r/n/r/ntype={0}/r/n/r/nmessage={1}/r/n/r/nstack=/r/n{2}/r/n/r/n",
currentException.GetType().FullName,
currentException.Message,
currentException.StackTrace);
}
EventLog Log = new EventLog();
Log.Source = _sourceName;
Log.WriteEntry(message.ToString(), EventLogEntryType.Error);
}
}
}
⑵. 打开Visual Studio 2005的命令提示行窗口
输入Type sn.exe -k key.snk后回车
输入Type csc /t:library /r:system.web.dll,system.dll /keyfile:key.snk UnhandledExceptionModule.cs后回车
输入gacutil.exe /if UnhandledExceptionModule.dll后回车
输入ngen install UnhandledExceptionModule.dll后回车
输入gacutil /l UnhandledExceptionModule后回车并将显示的”强名称”信息复制下来
⑶. 打开ASP.net应用程序的Web.config文件,将下面的XML加到里面。注意:不包括”[]”,①可能是添加到<httpModules></httpModules>之间。
<add name="UnhandledExceptionModule" type="WebMonitor.UnhandledExceptionModule, [这里换为上面复制的强名称信息]" />
三、微软并不建议的解决方案
打开位于 %WINDIR%/Microsoft.NET/Framework/v2.0.50727 目录下的 Aspnet.config 文件,将属性 legacyUnhandledExceptionPolicy 的 enabled 设置为 true
四、跳出三界外——ActiveX
ActiveX 的特点决定了不可能去更改每个客户端的设置,采用 B/S 解决方案里的第 3 种方法也不行,至于行不通的原因,我想可能是因为 ActiveX 的子控件产生的异常直接
被 CLR 截获了,并没有传到最外层的 ActiveX 控件,这只是个人猜测,如果有清楚的朋友,还望指正。
最终,我也没找到在 ActiveX 情况的解决方法,但这却是我最需要的,无奈之下,重新检查代码,发现了其中的问题:在子线程中创建了控件,又将它添加到了主线程的 UI 上。
以前遇到这种情况,系统就会报错了,这次居然可以蒙混过关,最搞不懂的是在 framework 2.0 的 C/S 结构下也没有报错,偏偏在 IE(ActiveX) 里挂了。唉,都是宿主惹的祸。
http://blog.csdn.net/fxfeixue/article/details/4466899
原文:
【转】CLR20R3 程序终止的几种解决方案相关推荐
- clr20r3 mysql.data_C# CLR20R3 程序终止的几种解决方案
这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理异常(例如,线程池线程中的未处理异常)提供支撑,而 Framework 2.0 版中,公共语言运行库允许线程中的多数未处 ...
- clr20r3 system.InvalidOperationException 程序终止的几种解决方案
这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理异常(例如,线程池线程中的未处理异常)提供支撑,而 Framework 2.0 版中,公共语言运行库允许线程中的多数未处 ...
- win7 clr20r3程序终止_mscorsvw.exe是什么进程 win7系统怎么禁用mscorsvw.exe进程【禁用方法】...
最近有位win7系统用户发现电脑运行速度越来越慢了,网络卡到不行,打开任务管理器发现cpu内存都要被mscorsvw.exe进程占满了,那么mscorsvw.exe是什么进程呢?win7系统怎么禁用m ...
- xp卡在正在应用计算机设置,XP系统经常提示“应用程序正在运行”的两种解决方案...
相信不少windowsxp系统用户都遇到这样的问题,那就是你的电脑开机后,会自动弹出指示器提示框"应用程序正在运行".那么,这种时候我们要怎么操作呢?下面,系统城小编就和大家介绍一 ...
- 微信小程序中嵌套html_在微信小程序中渲染HTML内容3种解决方案及分析与问题解决...
大部分Web应用的富文本内容都是以HTML字符串的形式存储的,通过HTML文档去展示HTML内容自然没有问题.但是,在微信小程序(下文简称为「小程序」)中,应当如何渲染这部分内容呢? 在微信小程序中渲 ...
- 实现微信小程序直播的2种方式|7大场景解决方案
ZEGO 微信小程序直播SDK 可以在微信小程序中提供实时音视频直播服务,从而实现电商直播/在线教育/在线问诊/视频客服等各种业务场景.但是由于微信小程序的官方限制,在某些场景下需要额外使用 ZEGO ...
- iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用(上)
2017-07-08 remember17 Cocoa开发者社区 目的 本文主要是分享iOS多线程的相关内容,为了更系统的讲解,将分为以下7个方面来展开描述. 多线程的基本概念 线程的状态与生命周期 ...
- python编程8g的内存够么_详解解决Python memory error的问题(四种解决方案)
昨天在用用Pycharm读取一个200+M的CSV的过程中,竟然出现了Memory Error!简直让我怀疑自己买了个假电脑,毕竟是8G内存i7处理器,一度怀疑自己装了假的内存条....下面说一下几个 ...
- Java开发Web Service的几种解决方案
转自:http://blog.csdn.net/zolalad/article/details/25158995 Java开发中经常使用到的几种WebService技术实现方案 随着异构系统互联需求的 ...
最新文章
- tomcat 配置方法
- FCS编程之NetConnect对象
- POJ - 2201 Cartesian Tree(笛卡尔树-单调栈/暴跳父亲)
- GAN实现半监督学习
- [AtCoder Regular Contest 125] A-F全题解
- 教程:在 VM Depot 中查找 Azure 可用的虚拟机镜像
- mft按钮设计_哈汽机组660MW超临界空冷机组ETS设计及逻辑说明
- java 性能瓶颈_如何通过 Java 线程堆栈来进行性能瓶颈分析?
- python第三十二天-----算法
- 2个月快速通过PMP证书的经验分享
- wpsmac历史版本_wps office下载
- 分享:破解还原精灵的几个小技巧(转)
- jwplayer android m3u8,播放上jwplayer M3U8文件,而RTMP
- APS系统在注塑行业的应用
- Qt::WA_DeleteOnClose介绍与注意事项
- [足式机器人]Part1 三维空间中的跳行Ch03——【Legged Robots that Balance 读书笔记】
- 绝了,hutool导出excel 图片居然没有调用方法
- 记一次内网SSH后门误报事件
- Linux ar命令(更改静态库相关属性信息)
- HTML核心(1)- 第一个网页