title: calling a synchronous method asynchronously
body: <p>I have a simple code where I'm trying to get a better understanding of how a method can be called asynchronously in C#.</p>
  
  <p>I have a function called Function1 which I want to run <em>asynchronously</em></p>
  
  <pre><code>static void Function1(out int threadId)
  {
      Console.WriteLine("I'm inside Function 1. Going to sleep for 7 seconds...");
      Thread.Sleep(7000);
      Console.WriteLine("I'm awake now");
      threadId = Thread.CurrentThread.ManagedThreadId;
  }
  </code></pre>
  
  <p>Then I have a second function Function2 which I want to run normally (synchronously)</p>
  
  <pre><code>static void Function2()
  {
      Thread.Sleep(3000);
      Console.WriteLine("I'm inside Function 2");
  }
  </code></pre>
  
  <p>I also created a delegate with the same method signature as Function1</p>
  
  <pre><code>delegate void AsyncMethodCaller(out int threadId);
  </code></pre>
  
  <p>And here is my main calling method:</p>
  
  <pre><code>static void Main(string[] args)
  {
      int threadId;
      AsyncMethodCaller caller = new AsyncMethodCaller(Function1);
      caller.BeginInvoke(out threadId, null, null);
      Function2();
  }
  </code></pre>
  
  <p>In my main method, I expect Function1 to start running asynchronously; and then <strong>without</strong> waiting on it to finish, Function2 executes. So I expect the following output:</p>
  
  <blockquote>
    <p>I'm inside the asynchronous Function 1. Going to sleep for 7
    seconds... <br>I'm inside Function 2<br> I'm awake now</p>
  </blockquote>
  
  <p>Instead I just get the following output:</p>
  
  <blockquote>
    <p>I'm inside the asynchronous Function 1. Going to sleep for 7
    seconds... <br>I'm inside Function 2</p>
  </blockquote>
  
  <p>Why is my expectation different from reality? why is the "I'm awake now" line never reached?</p>
  
  <p>Thank you</p>