using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace FirstWP8App
{
	static class Extensions
	{
		public static Task<Stream> DownloadStreamTask(this WebClient webClient, Uri uri)
		{
			var tcs = new TaskCompletionSource<Stream>();

			webClient.OpenReadCompleted += (s, e) =>
			{
				if (e.Error != null)
					tcs.SetException(e.Error);
				else
					tcs.SetResult(e.Result);
			};

			webClient.OpenReadAsync(uri);

			return tcs.Task;
		}
	}
}
