title: How to know if Task or Void method is still running when using Async
body: <p>I am trying to use async and await and my problem is that it wont wait for the background process to finish. Maybe you are wondering why don't I just run the application synchronously?
  I'm trying finish a part of my task ASAP and the rest can be awaited as shown in this example.
  Thank you for the help guys! =)</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Run();
          //Problem or Maybe Not? Needs this 
          //So that my application won't close immediately
          Console.ReadLine();
      }
  
      private async static void Run()
      {
          Task&lt;bool&gt; TBool = ProcessRecords();
  
          if (await TBool)
          {
              //Problem #1 This Line Doesn't Show
              Console.WriteLine("End of Code");
              //SHould be safe to close the application by Now
              //But goes through here not waiting for the return
              //of the last process.
  
              Environment.Exit(0);
  
              //My temporary solution is to indicate a Task.Delay(80000)
              //to make sure that all the logging in the background
              //are all done. I dont know if there is a function that shows
              //that there are task running on the background or any 
              //other workaroung will help. =) thanks
          }
  
      }
  
      private async static Task&lt;bool&gt; ProcessRecords()
      {
          Task newTask = null;
          //Loop through all the records and send
          //all the records to MQ ASAP
          for (int x = 0; x &lt; 10; x++)
          {
              //I wont wait for this Task so
              //I can send the next record
              newTask = SendToMQ(x);
          }
          //I only need to wait for the last Task to
          //indicate that I can exit the system
          //as soon as it finish
          //Problem #2 The Console.WriteLine doesnt show the last record.
          await newTask;
          return true;
      }
  
      private async static Task SendToMQ(int count)
      {
          //actual sending of message (Important)
          await Task.Delay(1000);
          //Process of Logging Connect to DB etc, (Not so Important, but takes most of the time)
          await LoggingRecord();
          Console.Clear();
          Console.WriteLine("Done Processing  " + count.ToString() + " records");
      }
  
      //Logging of each record
      private async static Task LoggingRecord()
      {
          await Task.Delay(5000);
          //Problem #3 This Line Doesn't Show
          Console.WriteLine("Last Log Finished");
      }
  }
  </code></pre>