// this signature is to match SendOrPostCallback public void LaunchMessageImpl(object ignoredState) { // now invoke our taskFunction and store the returned task ReturnedTask = taskFunction.Invoke(); if (ReturnedTask != null) { // register a continuation with the task, which will shut down the loop when the task completes. ReturnedTask.ContinueWith(delegate { postingContext.RequestMessageLoopTermination(); }, TaskContinuationOptions.ExecuteSynchronously); } else { // the delegate returned a null Task (VB/C# compilers never do this for async methods) // we don't have anything to register continuations with in this case, so exit out of the message loop // immediately Application.ExitThread(); } }