Merge pull request #1857 from slluis/fix-assembly-resolver
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / AsyncResult.cs
index 5ff6d62f134fb610f783082e9ec0ab9407b899c5..ff43f8dc0d0301c6df76aaf30b3e6e175dcdb352 100644 (file)
@@ -62,6 +62,7 @@ public class AsyncResult : IAsyncResult, IMessageSink, IThreadPoolWorkItem {
        IMessageCtrl message_ctrl;
 #pragma warning restore
        IMessage reply_message;
+       WaitCallback orig_cb;
        
        internal AsyncResult ()
        {
@@ -69,10 +70,28 @@ public class AsyncResult : IAsyncResult, IMessageSink, IThreadPoolWorkItem {
 
        internal AsyncResult (WaitCallback cb, object state, bool capture_context)
        {
+               orig_cb = cb;
+               if (capture_context) {
+                       var stackMark = default (StackCrawlMark);
+                       current = ExecutionContext.Capture (
+                               ref stackMark,
+                               ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase);
+                       cb = delegate {
+                               ExecutionContext.Run(current, ccb, this, true);
+                       };
+               }
+
                async_state = state;
                async_delegate = cb;
-               if (capture_context)
-                       current = ExecutionContext.Capture ();
+       }
+
+       static internal ContextCallback ccb = new ContextCallback(WaitCallback_Context);
+
+       static private void WaitCallback_Context(Object state)
+       {
+               AsyncResult obj = (AsyncResult)state;
+               WaitCallback wc = obj.orig_cb as WaitCallback;
+               wc(obj.async_state);
        }
 
        public virtual object AsyncState