Convertire WebClient in TPL in .NET 4.0?

Ho il seguente codice webclient,

public static void PostDataAsync(string url, NameValueCollection parameters, object state, Action callback) { var client = new WebClient(); client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; client.UploadValuesCompleted += new UploadValuesCompletedEventHandler(callback); client.UploadValuesAsync(new Uri(url), null, parameters, state); } 

Voglio convertire questo in attività. Sto cercando di farlo con Task.Factory.FromAsync ma non è riuscito

Ecco la risposta che ho trovato,

 public static Task PostDataAsync(string url, NameValueCollection parameters, object state) { var tcs = new TaskCompletionSource(state: state); var client = new WebClient(); client.UploadValuesCompleted += (obj, args) => { if (args.Cancelled) tcs.SetCanceled(); else if (args.Error != null) tcs.SetException(args.Error); else tcs.SetResult(args.Result); }; client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; client.UploadValuesAsync(new Uri(url), null, parameters, state); return tcs.Task; }