title: Async Await Timeout
body: <p>I am writing a simple method that validates a webpage url is real and returns either True or False.</p>
  
  <p>Using the new async await functions in .NET 4.5 this now seems ridiculously easy, but how do I set a timeout for async?</p>
  
  <pre><code>''' &lt;summary&gt;
  ''' Returns True if a webpage is valid.
  ''' &lt;/summary&gt;
  ''' &lt;param name="url"&gt;Url of the webpage.&lt;/param&gt;
  Private Async Function VailiateWebpageAsync(url As String) As Task(Of Boolean)       
      Dim httpRequest As HttpWebRequest
      Dim httpResponse As HttpWebResponse
  
      httpRequest = CType(WebRequest.Create(url), HttpWebRequest)
      httpRequest.Method = "HEAD" 'same as GET but does not return message body in the response
  
      Try
          httpResponse = CType(Await httpRequest.GetResponseAsync, HttpWebResponse)
      Catch ex As Exception
          httpResponse = Nothing
      End Try
  
      If Not IsNothing(httpResponse) Then
          If httpResponse.StatusCode = HttpStatusCode.OK Then
              httpResponse.Dispose()
              Return True
          End If
      End If
  
      If Not IsNothing(httpResponse) Then httpResponse.Dispose()
      Return False
  End Function
  </code></pre>