Example program of downloading a web page async in C#.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace IOBasedTasks
{
class Program
{
static void Main(string[] args)
{
Task<string> downloadTask = DownloadWebPageAsync("https://jonlabelle.com");
Console.Write("Downloading");
while (!downloadTask.IsCompleted)
{
Console.Write(".");
Thread.Sleep(100);
}
Console.WriteLine();
Console.WriteLine(downloadTask.Result);
}
private static async Task<string> DownloadWebPageAsync(string url)
{
WebRequest request = WebRequest.Create(url);
WebResponse response = await request.GetResponseAsync();
using (var reader = new StreamReader(response.GetResponseStream()))
{
string result = await reader.ReadToEndAsync();
return result;
}
}
}
}