Task <AvalonEditTextOutput> DecompileAsync(DecompilationContext context, int outputLengthLimit) { Debug.WriteLine("Start decompilation of {0} tree nodes", context.TreeNodes.Length); TaskCompletionSource <AvalonEditTextOutput> tcs = new TaskCompletionSource <AvalonEditTextOutput>(); if (context.TreeNodes.Length == 0) { // If there's nothing to be decompiled, don't bother starting up a thread. // (Improves perf in some cases since we don't have to wait for the thread-pool to accept our task) tcs.SetResult(new AvalonEditTextOutput()); return(tcs.Task); } Thread thread = new Thread(new ThreadStart( delegate { try { AvalonEditTextOutput textOutput = new AvalonEditTextOutput(); textOutput.LengthLimit = outputLengthLimit; DecompileNodes(context, textOutput); textOutput.PrepareDocument(); tcs.SetResult(textOutput); } catch (OperationCanceledException) { tcs.SetCanceled(); } catch (Exception ex) { tcs.SetException(ex); } })); thread.Start(); return(tcs.Task); }