title: ASP.NET Webforms with async/await
body: <p>My Webforms application which is based on .Net 4.6 has to use the async/await-functionality quite extensively. Because I'm quite new to this async/await topic I read quite a lot of best practices like <a href="https://msdn.microsoft.com/en-us/magazine/jj991977.aspx" rel="noreferrer">this</a> or <a href="https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45" rel="noreferrer">this</a>. But I still have some questions for which I haven't found any clear informations.</p>
  
  <ol>
  <li><p>Regarding Page-Lifecycle-Events: I know that e.g. for the Page_Load-Event it's best practice to avoid async void-methods and register such methods like this:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
     PageAsyncTask pageAsyncTask = new PageAsyncTask(SomeAsyncMethod);
     Page.RegisterAsyncTask(pageAsyncTask);
     //Method to invoke the registered async-methods immedietly and not after the PreRender-Event
     Page.ExecuteRegisteredAsyncTasks();
   }
  </code></pre>
  
  <p>My problem is that I want to call the async-method as soon as I registered it and not after the OnPreRender-event. This should be achieved by calling the ExecuteRegisteredAsyncTasks()-method. But in my case this has <strong>no effect</strong> and the async-method is still invoked after the PreRender-event. But why?</p></li>
  <li><p>Regarding Control-Events: Is it better to register async-methods the same way I mentioned in the code-example above or can I use the async-void signature like:</p>
  
  <pre><code>protected async void OnClick(object sender, EventArgs e)
  {
  await SomeAsyncMethod();
  }
  </code></pre>
  
  <p>I found both examples but no clear informations which is the better solution and why.</p></li>
  <li><p>Regarding Context and ConfigureAwait, it seems to be best practice to use <code>await SomeAsyncMethod.ConfigureAwait(false)</code> for a better performance and where the context is not important and not to use it where the context e.g. when manipulating GUI elements. But in my case it seems to make no difference if I call  <code>await SomeAsyncMethod.ConfigureAwait(false)</code> in my click-event. I can still manipulate my GUI-elements wihtout any problems. The example which I uses was this:</p>
  
  <pre><code>private async void button1_Click(object sender, EventArgs e)
  {
    button1.Enabled = false;
    try
    {
      await SomeAsyncMethod().ConfigureAwait(false);
    }
    finally
    {
      //Manipulating still works even it's another context
      button1.Enabled = true;
    }
  }
  </code></pre></li>
  </ol>
  
  <p>So I wonder why the manipulating of the GUI-elements still work and if I really should use ConfigureAwait(false) on every async-method where the context is not important, which is quite tedious. I wonder if this has something to do with the usage of the Ajax-Functionality by Telerik which I use for my Webapplication. But this is just an assumption.</p>