Skip to content

Instantly share code, notes, and snippets.

@smsohan
Created September 16, 2011 22:11
Show Gist options
  • Save smsohan/1223284 to your computer and use it in GitHub Desktop.
Save smsohan/1223284 to your computer and use it in GitHub Desktop.
C# async and await example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
namespace AsyncCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting...");
var smsohanDotCom = GetSmSohanDotCom();
Console.WriteLine("Passed the get line, status {0}", smsohanDotCom.IsCompleted);
Console.WriteLine(smsohanDotCom.Result.Length);
Console.WriteLine("Printed the length, status {0}", smsohanDotCom.IsCompleted);
Console.ReadLine();
}
private async static Task<String> GetSmSohanDotCom(){
Console.WriteLine("Before Waiting...");
var data = await new WebClient().DownloadStringTaskAsync(new Uri("http://smsohan.com"));
Console.WriteLine("Waiting...");
return data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment