Pārlūkot izejas kodu

2009-04-20 Rolf Bjarne Kvinge <[email protected]>

	* Thread.cs: For 2.1 ensure exceptions in threads are handled using the
	UnhandledException event mechanism in System.Windows.Application instead
	of crashing the application.


svn path=/trunk/mcs/; revision=132166
Rolf Bjarne Kvinge 16 gadi atpakaļ
vecāks
revīzija
42a3e10016

+ 6 - 0
mcs/class/corlib/System.Threading/ChangeLog

@@ -1,3 +1,9 @@
+2009-04-20  Rolf Bjarne Kvinge  <[email protected]>
+
+	* Thread.cs: For 2.1 ensure exceptions in threads are handled using the
+	UnhandledException event mechanism in System.Windows.Application instead
+	of crashing the application.
+
 2009-04-18  Mark Probst  <[email protected]>
 
 	* Thread.cs: Don't keep the current number formatter here because

+ 29 - 0
mcs/class/corlib/System.Threading/Thread.cs

@@ -733,6 +733,31 @@ namespace System.Threading {
 			}
 		}
 
+#if NET_2_1
+		private void StartSafe ()
+		{
+			try {
+				if (threadstart is ThreadStart) {
+					((ThreadStart) threadstart) ();
+				} else {
+					((ParameterizedThreadStart) threadstart) (start_obj);
+				}
+			} catch (Exception ex) {
+				try {
+					try {
+						var assembly = System.Reflection.Assembly.Load ("System.Windows, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e");
+						var application = assembly.GetType ("System.Windows.Application");
+						var method = application.GetMethod ("OnUnhandledException", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
+						method.Invoke (null, new object [] {null, ex});
+					} catch (Exception e) {
+						Console.WriteLine ("Unexpected exception while trying to report unhandled application exception: {0}", e);
+					}
+				} catch {
+				}
+			}
+		}
+#endif
+
 		public void Start() {
 			// propagate informations from the original thread to the new thread
 #if NET_2_0
@@ -748,7 +773,11 @@ namespace System.Threading {
 				_principal = CurrentThread._principal;
 
 			// Thread_internal creates and starts the new thread, 
+#if NET_2_1
+			if (Thread_internal((ThreadStart) StartSafe) == (IntPtr) 0)
+#else
 			if (Thread_internal(threadstart) == (IntPtr) 0)
+#endif
 				throw new SystemException ("Thread creation failed.");
 		}