Devin Martin avatar Devin Martin committed 8951763

Allow the continuation to run on a thread pool thread if no synchronization context is available

Comments (0)

Files changed (1)

AsyncOperation.cs

                         asyncAction();
                     if (continuation != null)
                     {
-                        context.Post(state =>
+                        this.PostDelegateToSynchronizationContext(() =>
                         {
                             try
                             {
                             {
                                 PostFinallyActionIfNeeded();
                             }
-                        }, null);
+                        });
                     }
                 }
                 catch (Exception e)
                     if (!finallyPosted)
                     {
                         finallyPosted = true;
-                        if (context != null)
-                            context.Post(state => finallyAction(), null);
-                        else
-                            ThreadPool.QueueUserWorkItem(state => finallyAction());
+                        PostDelegateToSynchronizationContext(finallyAction);
                     }
                 }
             }
                     if (!catchPosted)
                     {
                         catchPosted = true;
-                        if (context != null)
-                            context.Post(state => catchAction(e), null);
-                        else
-                            ThreadPool.QueueUserWorkItem(state => catchAction(e));
+                        PostDelegateToSynchronizationContext(() => catchAction(e));
                     }
                 }
             }
         }
+
+        /// <summary>
+        /// If a synchronization context was available when the object was constructed, post the delegate.  Otherwise queue it up on the thread pool.
+        /// </summary>
+        /// <param name="action">Delegate to queue</param>
+        private void PostDelegateToSynchronizationContext(Action action)
+        {
+            if (action != null)
+            {
+                if (context != null)
+                    context.Post(state => action(), null);
+                else
+                    ThreadPool.QueueUserWorkItem(state => action());
+            }
+        }
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.