Question : (NEWBIE)Visual Studio (Concurrent I/O operations error) (NEWBIE)

Hi,
I am brand new to winforms and visual studio as well as C#

I have started on my first program via youtube
http://www.youtube.com/watch?v=0ouZY7ap1MM

and it worked great, then i modified it by just a few lines by adding a while loop... and now its throwing an exception/error, as you can see here:

http://img59.imageshack.us/img59/4900/screenshotvs.png

I know i need to use
 wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

but other than that the whole thing confuses me... please helppppp!

Thanks!

Answer : (NEWBIE)Visual Studio (Concurrent I/O operations error) (NEWBIE)

You cannot use webclient to download multiple files asynchronously.If you have to download multiple files using webclient, then use DownloadFile and not DownloadFileAsync method.This will cause your application to stop responding till it the loop does not get over.
I am attaching the code with DownloadFile and it works fine.
Although if you to your front end running and download mutiple files using the same webclient , you can put the download code in the Background worker and use downloadfile for a webclient.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace webclient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        WebClient wc = new WebClient();
        private void button1_Click(object sender, EventArgs e)
        {
            Uri uri=new Uri("http://www.quack-ware.com/images/logo.jpg");
            //wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
            int i = 0;
            while (i < 20)
            {
               // wc.DownloadFileAsync(uri, "abc.jpg"+i.ToString());
                wc.DownloadFile(uri, "ax.jpg" + i.ToString());
                i++;
            }

        }

 /*       void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("done");
        }
*/
    }
}
Random Solutions  
 
programming4us programming4us