2009-06-10 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Wed, 10 Jun 2009 17:17:55 +0000 (17:17 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Wed, 10 Jun 2009 17:17:55 +0000 (17:17 -0000)
* Thread.cs: Refactor calling Moonlight's OnUnhandledException to
make sur the delegate it not called from a [SecuritySafeCritical]
caller.
* ThreadPool.cs: Reuse the above code for QueueUserWorkItem.
Original patch from Alan McGovern

svn path=/trunk/mcs/; revision=135866

mcs/class/corlib/System.Threading/ChangeLog
mcs/class/corlib/System.Threading/Thread.cs
mcs/class/corlib/System.Threading/ThreadPool.cs

index 0b03ecd2370e81abb0816c1c372bc1b3a0478179..5e6ebbd9c494c3cc4ae9c90a2edf2e5eedeea3ea 100644 (file)
@@ -1,3 +1,11 @@
+2009-06-10  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * Thread.cs: Refactor calling Moonlight's OnUnhandledException to 
+       make sur the delegate it not called from a [SecuritySafeCritical]
+       caller.
+       * ThreadPool.cs: Reuse the above code for QueueUserWorkItem. 
+       Original patch from Alan McGovern
+
 2009-06-10  Marek Safar  <marek.safar@gmail.com>
 
        * LockRecursionException.cs: New file.
index ead1be87751bcb5b4de0214cb667316d26573bb9..80e21692a3090ce62e97ba2db84628789edfc098 100644 (file)
@@ -37,6 +37,7 @@ using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.IO;
 using System.Collections;
+using System.Reflection;
 using System.Security;
 
 #if NET_2_0
@@ -752,17 +753,25 @@ namespace System.Threading {
                                        ((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 {
+                               MoonlightUnhandledException (ex);
+                       }
+               }
+
+               static MethodInfo moonlight_unhandled_exception = null;
+
+               static internal void MoonlightUnhandledException (Exception e)
+               {
+                       try {
+                               if (moonlight_unhandled_exception == null) {
+                                       var assembly = System.Reflection.Assembly.Load ("System.Windows, Version=2.0.5.0, Culture=Neutral, PublicKeyToken=7cec85d7bea7798e");
+                                       var application = assembly.GetType ("System.Windows.Application");
+                                       moonlight_unhandled_exception = application.GetMethod ("OnUnhandledException", 
+                                               System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
                                }
+                               moonlight_unhandled_exception.Invoke (null, new object [] { null, e });
+                       }
+                       catch {
+                               Console.WriteLine ("Unexpected exception while trying to report unhandled application exception: {0}", e);
                        }
                }
 #endif
index 5fd27d7d5779491d89fab344db05c82e99f60177..feca0234aca1995bd22135020e8ca3128c889657 100644 (file)
@@ -90,14 +90,12 @@ namespace System.Threading {
                        
                public static bool QueueUserWorkItem (WaitCallback callBack)
                {
-                       IAsyncResult ar = callBack.BeginInvoke (null, null, null);
-                       if (ar == null)
-                               return false;
-                       return true;
+                       return QueueUserWorkItem (callBack, null);
                }
 
                public static bool QueueUserWorkItem (WaitCallback callBack, object state)
                {
+                       callBack = MoonlightHandler (callBack);
                        IAsyncResult ar = callBack.BeginInvoke (state, null, null);
                        if (ar == null)
                                return false;
@@ -218,5 +216,21 @@ namespace System.Threading {
                {
                        throw new NotImplementedException ();
                }
+
+               static WaitCallback MoonlightHandler (WaitCallback callback)
+               {
+#if NET_2_1
+                       return delegate (object o) {
+                               try {
+                                       callback (o);
+                               } 
+                               catch (Exception ex) {
+                                       Thread.MoonlightUnhandledException (ex);
+                               } 
+                       };
+#else
+                       return callback;
+#endif
+               }
        }
 }