locked
Downloading images async with .net 4

    Question

  • i have a list of images url now i want to download the images  images async and then wait for all of them to complete before the function exit e.g please Note that i am using .net 4

    void DownloadImages(List<string> lst_images)

    {

           //download images

         // wait for all images to download

    }


    • Edited by Haider.ali Wednesday, July 11, 2012 7:19 AM
    Wednesday, July 11, 2012 7:12 AM

Answers

  • using System;
    using System.Collections.Generic;
    using System.Net;
    using System.IO;
    using System.ComponentModel;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1 {   public partial class Form1 : Form
      {
        
    public Form1()
        {
          InitializeComponent();
        }
        
    public void DownloadImages(List<string> ImgList)
        {
        WC = 
    new WebClient();
        WC.DownloadFileCompleted += Wc_Complete;
        Stack = 
    new Stack<string>(ImgList);
        
    string CurImg = Stack.Pop();
        WC.DownloadFileAsync(
    new Uri(CurImg), Path.Combine(ImgDir, Path.GetFileName(CurImg)));
        }
        
    private string ImgDir;
        
    private WebClient WC;
        
    private Stack<string> Stack;
        
    public void Wc_Complete(object sender, AsyncCompletedEventArgs e)
        {
        
    if (Stack.Count > 0)
        {
          
    string CurImg = Stack.Pop();
          WC.DownloadFileAsync(
    new Uri(CurImg), Path.Combine(ImgDir, Path.GetFileName(CurImg)));
        }
        
    else
        {
          Stack = 
    null;
          WC.Dispose();
          
    MessageBox.Show("All images downloaded");
        }
        }
      }
    }


    • Marked as answer by Haider.ali Wednesday, July 11, 2012 9:41 AM
    Wednesday, July 11, 2012 9:36 AM

All replies

  • using System;
    using System.Collections.Generic;
    using System.Net;
    using System.IO;
    using System.ComponentModel;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1 {   public partial class Form1 : Form
      {
        
    public Form1()
        {
          InitializeComponent();
        }
        
    public void DownloadImages(List<string> ImgList)
        {
        WC = 
    new WebClient();
        WC.DownloadFileCompleted += Wc_Complete;
        Stack = 
    new Stack<string>(ImgList);
        
    string CurImg = Stack.Pop();
        WC.DownloadFileAsync(
    new Uri(CurImg), Path.Combine(ImgDir, Path.GetFileName(CurImg)));
        }
        
    private string ImgDir;
        
    private WebClient WC;
        
    private Stack<string> Stack;
        
    public void Wc_Complete(object sender, AsyncCompletedEventArgs e)
        {
        
    if (Stack.Count > 0)
        {
          
    string CurImg = Stack.Pop();
          WC.DownloadFileAsync(
    new Uri(CurImg), Path.Combine(ImgDir, Path.GetFileName(CurImg)));
        }
        
    else
        {
          Stack = 
    null;
          WC.Dispose();
          
    MessageBox.Show("All images downloaded");
        }
        }
      }
    }


    • Marked as answer by Haider.ali Wednesday, July 11, 2012 9:41 AM
    Wednesday, July 11, 2012 9:36 AM
  • just for info how about this (need your suggestion)

    private void button1_Click(object sender, EventArgs e)
            {
                List<string> errors = new List<string>();
                List<string> lst = new List<string>();
                lst.Add("DSC_0172.JPG");
                lst.Add("DSC_0146.JPG");
                lst.Add("DSC_0150.JPG");
                lst.Add("DSC_9155.JPG");
                lst.Add("DSC_0173.JPG");
                var p = Parallel.ForEach<string>(lst, (x) => { if (!DownloadFile(x)) { errors.Add(x + "Not Found"); } });
                
                while (true)
                {
                    if (p.IsCompleted)
                    {
                        break;
                    }
                }
                foreach (var item in errors)
                {
                    textBox3.AppendText(item + "\n");
                }
            }

            bool DownloadFile(string FileName)
            {
                try
                {
                    WebClient wc = new WebClient();
                    Stream s = wc.OpenRead(@"http://localhost:13802/images/" + FileName);
                    var img = new Bitmap(s);
                    img.Save(FileName);
                    return true;
                }
                catch (Exception)
                {
                    return false;
     
                }
            }

    Thursday, July 12, 2012 6:51 AM