title: Force console application to print even when busy in C#
body: <p>I have a simple console application as follows:</p>
  
  <pre><code>private static void Main (string [] args)
  {
      System.Console.WriteLine("Initializing context...");
      Database.Context.Initialize();
  
      System.Console.WriteLine("Creating server...");
      var server = new Server();
      server.OnStarting += Server_OnStarting;
      server.OnStarted += Server_OnStarted;
  
      System.Console.WriteLine("Starting server...");
      server.Start();
  }
  
  private static void Server_OnStarting (object sender, EventArgs e)
  {
      System.Console.WriteLine("Server_OnStarting.");
  }
  
  private static void Server_OnStarted (object sender, EventArgs e)
  {
      System.Console.WriteLine("Server_OnStarted.");
  }
  </code></pre>
  
  <p><strong>SERVER START METHOD:</strong></p>
  
  <pre><code>public bool Start ()
  {
      bool result = false;
  
      lock (this._SyncRoot)
      {
          if (!this.Running)
          {
              this.Stopwatch.Reset();
  
              this.RaiseOnStarting();
  
              this.Running = true;
              this.Terminate = false;
              this.ThreadObject = new System.Threading.Thread(new System.Threading.ThreadStart(this.ProcessInternal));
  
              this.Stopwatch.Restart();
              this.ThreadObject.Start();
  
              this.RaiseOnStarted();
  
              result = true;
          }
      }
  
      return (result);
  }
  </code></pre>
  
  <p><strong>OUTPUT</strong>:</p>
  
  <p><img src="https://i.stack.imgur.com/ZqpI0.png" alt="Console Output"></p>
  
  <p>Out of the three <code>Console.WriteLine</code> calls in <code>Main</code>, only the first gets printed immediately. This is understandable since everything in <code>Database.Context.Initialize</code> happens on the same thread. However, the printing of <code>Creating server...</code> and <code>Starting server...</code> are delayed and clump together with the <code>Server_OnStarting</code> and <code>Server_OnStarted</code> messages. The call to the constructor is not threaded but the call to server.Start internally launches a thread, initializes some sockets, etc. However, the events <code>OnStarting</code> and <code>OnStarted</code> are fired on the <strong>SAME</strong> thread so the threading aspect of it SHOULD NOT matter.</p>
  
  <p>It seems, that after <code>Initializing context...</code> is printed, no matter where I put the call to <code>Console.WriteLine</code>, it does not update the console window until server.Start returns after firing both events.</p>
  
  <p>Is there a way to force the console to update while waiting for the server to start?</p>