Return from Async task - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Async

Description

Return from Async task

Demo Code

using System;//from   w w w .j  a  v  a 2s  .c o  m
using System.ComponentModel;
using System.Net.Http;
using System.Threading.Tasks;
class ReturnFromAsync
{
   static readonly HttpClient client = new HttpClient();
   static async Task<int> GetPageLengthAsync(string url)
   {
      Task<string> fetchTextTask = client.GetStringAsync(url);
      int length = (await fetchTextTask).Length;
      return length;
   }
   static void Main()
   {
      Task<int> lengthTask = GetPageLengthAsync("http://book2s.com");
      Console.WriteLine(lengthTask.Result);
   }
}

Related Tutorials