Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program download from a website the same image every minute.

It was working fine for the last 24 hours more or less untill now it was throwing this exception.

In the top of form1 i did:

C#
WebClient Client;


In the constructor i did:

C#
Client = new WebClient();
Client.DownloadFileCompleted += Client_DownloadFileCompleted;
Client.DownloadProgressChanged += Client_DownloadProgressChanged;


Then i have this method i call it from a timer tick event every minute:

C#
private void fileDownloadRadar()
        {
            if (Client.IsBusy == true)
            {
                Client.CancelAsync();
            }
            else
            {
                Client.DownloadProgressChanged += Client_DownloadProgressChanged;
                Client.DownloadFileAsync(myUri, combinedTemp);
            }
        }


Every minutes it's downloading an image from a website same image each time. It was all working for more then 24 hours no problems untill now throwing this exception in the download completed event:

C#
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

            if (e.Error != null)
            {
                timer1.Stop();
                span = new TimeSpan(0, (int)numericUpDown1.Value, 0);
                label21.Text = span.ToString(@"mm\:ss");
                timer3.Start();
            }
            else if (!e.Cancelled)
            {
                label19.ForeColor = Color.Green;
                label19.Text = "חיבור האינטרנט והאתר תקינים";
                label19.Visible = true;
                timer3.Stop();
                if (timer1.Enabled != true)
                {
                    if (BeginDownload == true)
                    {
                        timer1.Start();
                    }
                }                
                bool fileok = Bad_File_Testing(combinedTemp);
                if (fileok == true)
                {
                    File1 = new Bitmap(combinedTemp);
                    bool compared = ComparingImages(File1);
                    if (compared == false)
                    {

                        DirectoryInfo dir1 = new DirectoryInfo(sf);
                        FileInfo[] fi = dir1.GetFiles("*.gif");
                        last_file = fi[fi.Length - 1].FullName;
                        string lastFileNumber = last_file.Substring(82, 6);
                        int lastNumber = int.Parse(lastFileNumber);
                        lastNumber++;
                        string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
                        identicalFilesComparison = File_Utility.File_Comparison(combinedTemp, last_file);
                        if (identicalFilesComparison == false)
                        {
                            string newfile = Path.Combine(sf, newFileName);
                            File.Copy(combinedTemp, newfile);
                            LastFileIsEmpty();
                        }
                    }
                    if (checkBox2.Checked)
                    {
                        simdownloads.SimulateDownloadRadar();
                    }
                }
                else
                {
                    File.Delete(combinedTemp);
                }
                File1.Dispose();
            }
        }



Now it stopped inside the if(e.Error != null) On the line: timer1.Stop();

Then i see on the Error the error: This is the stack trace:

at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)

I tried to change the fileDownloadRadar method to this to release the client every time:

C#
private void fileDownloadRadar()
        {
            using (WebClient client = new WebClient())
            {
                if (client.IsBusy == true)
                {
                    client.CancelAsync();
                }
                else
                {

                    client.DownloadFileAsync(myUri, combinedTemp);

                }
            }
        }


The problem is that in the constructor i'm using Client and here it's client two different Webclient variables.

How can i solve this and the exception ?
Posted
Comments
ZurdoDev 30-Oct-14 14:08pm    
I have seen this error when forms authentication is blocking a request. Google the exact error and you'll find a number of possible causes.
gggustafson 31-Oct-14 10:38am    
Client.DownloadProgressChanged += Client_DownloadProgressChanged;

should not appear in fileDownloadRadar. You've already declared it. Try removing it. If that fails, stop your application every 20 hours or so and restart it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900