Member Avatar for Malaoshi

Hi everybody,

I am making a tool to download a Chinese newspaper and so far it seems to be fine, but the programm seems to be freezing while downloading and just after it has finished it's task it will be back to normal - what can I do?

And how do I add an option for pause and cancel?

Here is my code:

public partial class Form1 : Form
    {
        private string datum;
        private string remoteUri = "http://nf.nfdaily.cn/epaper/21cn/IMAGE/";
        private int countStart, countEnd;
        private string path;

        public Form1()
        {
            InitializeComponent();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            DateTimeFormatInfo myDTFI = new CultureInfo("de-DE", false).DateTimeFormat;
            datum = Convert.ToString(monthCalendar1.SelectionRange.Start.ToString("yyyyMMdd", myDTFI));
            tagLabel.Text = Convert.ToString(monthCalendar1.SelectionRange.Start.ToString("dd. MMMM yyyy", myDTFI));
        }


        private void start_Click(object sender, EventArgs e)
        {
            if (tagLabel.Text=="Noch kein Datum ausgewählt!")
            {
                MessageBox.Show ("Bitte gib ein Datum an!");
                return;
            }


            // Specify the directory you want to manipulate.
            path = PathBox.Text + @"\" + datum + @"\";

            try
            {
                // Determine whether the directory exists.
                if (Directory.Exists(path))
                {
                    MessageBox.Show("Dieser Ordner besteht bereits: " + path + "\nDer Download geht aber ungestört weiter.");
                }
                else
                {
                    // Try to create the directory.
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    label4.Text = "Der Ordner wurde erfolgreich erstellt: " + Directory.GetCreationTime(path);
                    label4.Refresh();
                }
            }
            catch
            {
                MessageBox.Show("Da gab es wohl ein Problem!");
            }
            
                    try
                    {
                        countStart = Convert.ToInt32(nummerBox1.Text);
                        countEnd = Convert.ToInt32(nummerBox2.Text);
                    }
                    catch
                    { 
                        countStart = 2;
                        countEnd = 37;
                    }
            runterLaden();
           
            
        }



        private void runterLaden()
        {
            for (int i = countEnd; countStart <= i; countStart++)
            {
                downLabel.Refresh();
                Convert.ToString(countStart);
                string fileName = null, myStringWebResource = null, testString = null;

                if (countStart < 10)
                {
                    fileName = "J0" + countStart + "-B.JPG";
                }
                else
                {
                    fileName = "J" + countStart + "-B.JPG";
                }

                System.Net.WebClient myWebClient = new WebClient();
                myStringWebResource = remoteUri + datum + "/" + fileName;
                testString = remoteUri + datum + "/" + "J01-B.JPG";
                downLabel.Text = myStringWebResource + " ==> downloading";
                downLabel.Refresh();
                pfadLabel.Text = path;
                
                try
                {
                    myWebClient.DownloadFile(testString, path + fileName);
                }
                catch
                {
                    MessageBox.Show("Diese Zeitung scheint nicht online zu sein,\nbitte überprüfe Deine Einstellungen!");
                    Directory.Delete(path, true);
                    return; }

                try
                {
                    myWebClient.DownloadFile(myStringWebResource, path + fileName);
                }
                catch { }
                                
            };
        }


        
    }

So most important is to fix the freezing problem.

Can anyone help me on that?

Thanks,
Malaoshi

Member Avatar for Malaoshi

Hey there,

found a solution: DownloadFileAsync!

But a new problem came up, what can I do to load one file after another...or make the progressbar count for all the files? Or show which file is being downloaded at the moment?

private void runterLaden()
        {
            for (int i = countEnd; countStart <= i; countStart++)
            {
                Convert.ToString(countStart);
                
                if (countStart < 10)
                {
                    fileName = "J0" + countStart + "-B.JPG";
                }
                else
                {
                    fileName = "J" + countStart + "-B.JPG";
                }

                System.Net.WebClient myWebClient = new WebClient();
                myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                myWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                
                myStringWebResource = remoteUri + datum + "/" + fileName;
                pfadLabel.Text = path;
                
                try
                {

                    myWebClient.DownloadFileAsync(new Uri(myStringWebResource), path + fileName);
                   
                }
                catch { }
                                
            };
        }

        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            downLabel.Text = fileName + " ==> fertig";
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.