clr20r3 mysql.data_C# CLR20R3 程序终止的几种解决方案
這是因為.NET Framework 1.0 和 1.1 這兩個版本對許多未處理異常(例如,線程池線程中的未處理異常)提供支撐,而 Framework 2.0 版中,公共語言運行庫允許線程中的多數未處理異常自然繼續。在多數情況下,這意味著未處理異常會導致應用程序終止。
一、C/S 解決方案(以下任何一種方法)
1. 在應用程序配置文件中,添加如下內容:
1
2
3
4
5
2. 在應用程序配置文件中,添加如下內容:
1
2
3
4
5
3. 使用Application.ThreadException事件在異常導致程序退出前截獲異常。示例如下:
1 [SecurityPermission(SecurityAction.Demand, Flags =SecurityPermissionFlag.ControlAppDomain)]2 public static void Main(string[] args)3 {4 Application.ThreadException += newThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);5 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);6 AppDomain.CurrentDomain.UnhandledException += newUnhandledExceptionEventHandler(CurrentDomain_UnhandledException);7
8 Application.Run(newErrorHandlerForm());9 }
日志事件
1 private static void Form1_UIThreadException(objectsender, ThreadExceptionEventArgs t)2 {3 DialogResult result =DialogResult.Cancel;4 try
5 {6 result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception);7 }8 catch
9 {10 try
11 {12 MessageBox.Show("Fatal Windows Forms Error",13 "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);14 }15 finally
16 {17 Application.Exit();18 }19 }20
21 if (result ==DialogResult.Abort)22 Application.Exit();23 }24
25
26 //由于 UnhandledException 無法阻止應用程序終止,因而此示例只是在終止前將錯誤記錄在應用程序事件日志中。
27 private static void CurrentDomain_UnhandledException(objectsender, UnhandledExceptionEventArgs e)28 {29 try
30 {31 Exception ex =(Exception)e.ExceptionObject;32 string errorMsg = "An application error occurred. Please contact the adminstrator" +
33 "with the following information:/n/n";34
35 if (!EventLog.SourceExists("ThreadException"))36 {37 EventLog.CreateEventSource("ThreadException", "Application");38 }39
40 EventLog myLog = newEventLog();41 myLog.Source = "ThreadException";42 myLog.WriteEntry(errorMsg + ex.Message + "/n/nStack Trace:/n" +ex.StackTrace);43 }44 catch(Exception exc)45 {46 try
47 {48 MessageBox.Show("Fatal Non-UI Error",49 "Fatal Non-UI Error. Could not write the error to the event log. Reason:"
50 +exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);51 }52 finally
53 {54 Application.Exit();55 }56 }57 }58
59
60 private static DialogResult ShowThreadExceptionDialog(stringtitle, Exception e)61 {62 string errorMsg = "An application error occurred. Please contact the adminstrator" +
63 "with the following information:/n/n";64 errorMsg = errorMsg + e.Message + "/n/nStack Trace:/n" +e.StackTrace;65 returnMessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,66 MessageBoxIcon.Stop);67 }
二、B/S 解決方案(以下任何一種方法)
1. 在IE目錄(C:/Program Files/Internet Explorer)下建立iexplore.exe.config文件,內容如下:
1 <?xml version="1.0"?>
2
3
4
5
6
2. 不建議使用此方法,這將導致使用 framework 1.1 以后版本的程序在IE中報錯。
建立同上的配置文件,但內容如下:
1 <?xml version="1.0"?>
2
3
4
5
6
3. 這個比較繁瑣,分為三步:
⑴. 將下面的代碼保存成文件,文件名為UnhandledExceptionModule.cs,路徑是C:/Program Files/Microsoft Visual Studio 14/
1 usingSystem;2 usingSystem.Diagnostics;3 usingSystem.Globalization;4 usingSystem.IO;5 usingSystem.Runtime.InteropServices;6 usingSystem.Text;7 usingSystem.Threading;8 usingSystem.Web;9
10 namespaceWebMonitor {11 public classUnhandledExceptionModule: IHttpModule {12
13 static int _unhandledExceptionCount = 0;14
15 static string _sourceName = null;16 static object _initLock = new object();17 static bool _initialized = false;18
19 public voidInit(HttpApplication app) {20
21 //Do this one time for each AppDomain.
22 if (!_initialized) {23 lock(_initLock) {24 if (!_initialized) {25 string webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");26
27 if (!File.Exists(webenginePath)) {28 throw newException(String.Format(CultureInfo.InvariantCulture,29 "Failed to locate webengine.dll at '{0}'. This module requires .NET Framework 2.0.",30 webenginePath));31 }32
33 FileVersionInfo ver =FileVersionInfo.GetVersionInfo(webenginePath);34 _sourceName = string.Format(CultureInfo.InvariantCulture, "ASP.NET {0}.{1}.{2}.0",35 ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart);36
37 if (!EventLog.SourceExists(_sourceName)) {38 throw newException(String.Format(CultureInfo.InvariantCulture,39 "There is no EventLog source named '{0}'. This module requires .NET Framework 2.0.",40 _sourceName));41 }42
43 AppDomain.CurrentDomain.UnhandledException += newUnhandledExceptionEventHandler(OnUnhandledException);44
45 _initialized = true;46 }47 }48 }49 }50
51 public voidDispose() {52 }53
54 void OnUnhandledException(objecto, UnhandledExceptionEventArgs e) {55 //Let this occur one time for each AppDomain.
56 if (Interlocked.Exchange(ref _unhandledExceptionCount, 1) != 0)57 return;58
59 StringBuilder message = new StringBuilder("/r/n/r/nUnhandledException logged by UnhandledExceptionModule.dll:/r/n/r/nappId=");60
61 string appId = (string) AppDomain.CurrentDomain.GetData(".appId");62 if (appId != null) {63 message.Append(appId);64 }65
66 Exception currentException = null;67 for (currentException = (Exception)e.ExceptionObject; currentException != null; currentException =currentException.InnerException) {68 message.AppendFormat("/r/n/r/ntype={0}/r/n/r/nmessage={1}/r/n/r/nstack=/r/n{2}/r/n/r/n",69 currentException.GetType().FullName,70 currentException.Message,71 currentException.StackTrace);72 }73
74 EventLog Log = newEventLog();75 Log.Source =_sourceName;76 Log.WriteEntry(message.ToString(), EventLogEntryType.Error);77 }78 }79 }
⑵. 打開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加到里面。注意:不包括”[]”,①可能是添加到之間。
三、微軟并不建議的解決方案
打開位于 %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) 里掛了。唉,都是宿主惹的禍。
總結
以上是生活随笔為你收集整理的clr20r3 mysql.data_C# CLR20R3 程序终止的几种解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Leetcode】大神总结的链表常见面
- 下一篇: mysql导出表结构_mysql导入导出