title: How to block console from returning with WPF application attached to console while App is still running
body: <p>I have the following code in WPF entry point</p>
  
  <pre><code>public partial class App : Application
  {
      [DllImport("kernel32.dll")]
      static extern bool AttachConsole(int dwProcessId);
      private const int ATTACH_PARENT_PROCESS = -1;
  
      protected override void OnStartup(StartupEventArgs e)
      {
          if (e.Args.Length == 0)
          {
              // Launch GUI and pass arguments in case you want to use them.
              base.OnStartup(e);
              new MainWindow().ShowDialog();
          }
          else
          {
              //Do command line stuff
              if (e.Args.Contains("-i"))
              {
                  // redirect console output to parent process;
                  // must be before any calls to Console.WriteLine()
                  AttachConsole(ATTACH_PARENT_PROCESS);
                  var stopWatch = new Stopwatch();
                  stopWatch.Reset();
                  stopWatch.Start();
  
                  for (int i = 0; i &lt; 100; i++)
                  {
                      Console.WriteLine(i);
                  }
                  stopWatch.Stop();
                  var ts = stopWatch.Elapsed;
                  var elapsedTime = $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}";
                  Console.WriteLine("RunTime " + elapsedTime);               
              }
              else
              {
                  AttachConsole(ATTACH_PARENT_PROCESS);
                  Console.WriteLine("Incorrect arguments");
              }
          }
  
          Shutdown();
      }
  }
  </code></pre>
  
  <p>The prompt returns immediately as though my app runs in the background.</p>
  
  <p><a href="https://i.stack.imgur.com/ScuGe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ScuGe.png" alt="enter image description here"></a></p>
  
  <p><strong>I would expect the app to block the console from being returned until the process is completed.</strong></p>