Tuesday, March 18, 2014

Await a Task Completion Source Task for Async Data




The following can be modified to use yield return and observables
 private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await Test();
        }
        Task Test()
        {
            WebClient w = new WebClient();
            return w.DownloadStringTaskAsync("");
        }

        Task RunCodeBlockAsync()
        {
            var tcs = new TaskCompletionSource();
            WebClient w = new WebClient();
            w.DownloadStringCompleted +=
                (_, args) =>
                {
                    tcs.SetResult(args.Result);
                };
            return tcs.Task;
        }

No comments: