Example #1
0
        /// <summary>
        /// Version of load images using WhenAll. Not a good idea,
        /// since we can show something only when we have all results
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, EventArgs e)
        {
            var t1 = AsyncTaskModel.DownloadImageFromUrlAsync(url1.Text);
            var t2 = AsyncTaskModel.DownloadImageFromUrlAsync(url2.Text);
            var t3 = AsyncTaskModel.DownloadImageFromUrlAsync(url3.Text);

            Task.WhenAll(t1, t2, t3).
            ContinueWith(t => {
                if (t.Status == TaskStatus.Faulted)
                {
                    ShowErrors(t.Exception);
                }
                else
                {
                    pictureBox1.Image = t.Result[0];
                    pictureBox2.Image = t.Result[1];
                    pictureBox3.Image = t.Result[2];
                }
            });     //, TaskScheduler.FromCurrentSynchronizationContext());
        }
Example #2
0
        /// <summary>
        /// A version of handler made async to avoid explicit continuations
        /// that use the launch downloads in sequence. Not good!
        /// Note that the ConfigureAwait call results in an error since the
        /// continuation will not run on the UI thread!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void button3_Click(object sender, EventArgs e)
        {
            string[] sites =
            {
                url1.Text, url2.Text, url3.Text
            };
            Debug.WriteLine("Start in UI thread {0}",
                            Thread.CurrentThread.ManagedThreadId);
            PictureBox[] viewers = { pictureBox1, pictureBox2, pictureBox3 };
            int          index   = 0;

            foreach (string url in sites)
            {
                Image img =
                    await AsyncTaskModel.DownloadImageFromUrlAsync(url);

                Debug.WriteLine("Continuation on thread {0}",
                                Thread.CurrentThread.ManagedThreadId);
                viewers[index].Image = img;
                index++;
                status.Text = "Done with Success";
            }
        }