Devin Martin avatar Devin Martin committed ffbe2af

Prevent null reference exception in the async operation if no synchronization context is available

Comments (0)

Files changed (1)

AsyncOperation.cs

         bool catchPosted = false;
         bool finallyPosted = false;
 
+        /// <summary>
+        /// Create an instance of Async Operation.
+        /// </summary>
+        /// <remarks>
+        /// This must be done on the UI thread if continuations and error handling are to also occur on the UI thread.
+        /// </remarks>
+        /// <param name="asyncAction">The action to run in the background</param>
+        /// <param name="continuation">The continuation to run agains the current synchronization context</param>
+        /// <param name="catchAction">Error handling to run agains the current synchronization context</param>
+        /// <param name="finallyAction">Finally to run agains the current synchronization context</param>
         public AsyncOperation(Action asyncAction, Action continuation, Action<Exception> catchAction, Action finallyAction)
         {
             context = SynchronizationContext.Current;
             this.finallyAction = finallyAction;
         }
 
+        /// <summary>
+        /// Run the async action delegate provided upon construction and post any continuations to the synchronization context that was avilable at construction
+        /// </summary>
         public void Run()
         {
             Action runAction = () =>
                 }
             };
 
-            // grab a thread pool thread and run
-            runAction.BeginInvoke(null, null);
+            ThreadPool.QueueUserWorkItem(state => runAction());
         }
 
         private void PostFinallyActionIfNeeded()
                     if (!finallyPosted)
                     {
                         finallyPosted = true;
-                        context.Post(state => finallyAction(), null);
+                        if (context != null)
+                            context.Post(state => finallyAction(), null);
+                        else
+                            ThreadPool.QueueUserWorkItem(state => finallyAction());
                     }
                 }
             }
                     if (!catchPosted)
                     {
                         catchPosted = true;
-                        context.Post(state => catchAction(e), null);
+                        if (context != null)
+                            context.Post(state => catchAction(e), null);
+                        else
+                            ThreadPool.QueueUserWorkItem(state => catchAction(e));
                     }
                 }
             }
         }
     }
-}
+}
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.