title: Can't write to console in async C# code when called from powershell ISE
body: <p>I have a PowerShell script that needs to call C# to do work, some of it asynchronously.  While debugging some errors that only occur when the C# code is called from PowerShell, I noticed something I wasn't expecting.  In the C# code, if I made a call to <code>Console.WriteLine</code> it wrote to the PowerShell console as expected <strong>unless</strong> I was using PowerShell ISE and it was called after I did an await in an async function.</p>
  
  <p>Here's a sample script that shows the issue:</p>
  
  <pre><code>$source = @"
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading;
  using System.Threading.Tasks;
  
  public class TestClass
  {   
      public static int TestWrapper()
      {
          var task = TestAsync();
          task.Wait();
          return task.Result;
      }
  
      private static async Task&lt;int&gt; TestAsync()
      {
          Console.WriteLine("Before await");
          await Task.Run(() =&gt; { Thread.Sleep(2000); return; });
          Console.WriteLine("After await");
          return 2;
      }
  }
  
  "@
  
  Add-Type -TypeDefinition $source
  [TestClass]::TestWrapper()
  </code></pre>
  
  <p>In here, I'm just calling an asynchronous function, waiting for it to complete, and returning the result.  Inside the function, I just try to log before and after putting a thread to sleep for a few seconds before returning a value so that I know that the function fully executed.</p>
  
  <p>I would expect to see:</p>
  
  <pre><code>Before await
  After await
  2
  </code></pre>
  
  <p>And that's what I see when running as a C# console application or in basic PowerShell, but when running through PowerShell ISE, I see:</p>
  
  <pre><code>Before await
  2
  </code></pre>
  
  <p>Can anyone explain why that would be?</p>