0

I have a problem with the Wunderground forecast that I am using to retrieve data in c# program.

When I click to retrieve data once everything is working correctly but when I hit the button once more I am getting this error: Background Worker

Here is my code:

        private void bweather_DoWork(object sender, DoWorkEventArgs e)
        {
            string lat = Math.Round(deciLat).ToString();
            string lng = Math.Round(deciLon).ToString();
            string latlong = String.Format("{0},{1}", lat.Replace(',', '.'), lng.Replace(',', '.'));

            //Initialize Current as a new Day
            dow.Current = new WeatherLib.WDay();

            //Using Wunderground as the provider we populate the property with current data for the latlong entered into the textbox
            try
            {
                dow = WeatherLib.WProvider.Wunderground(latlong);
                writeToLogFile("Retrieve weather info successfully on: " + latlong);
            }
            catch (Exception ex)
            {
                writeToLogFile(ex.Message);
            }
        }

Here is the refresh button:

 private void weather_refresh_Click(object sender, EventArgs e)
        {
            writeToLogFile("Weather button pressed");
            weather_descripton.Clear();
            weather_speed_textbox.Clear();
            weather_tem_textbox.Clear();
            weather_rain_text.Clear();
            weather_wind_dir_textbox.Clear();
            weather_descripton.AppendText("Searching.......");
            if (!bweather.IsBusy)
            {
                bweather.CancelAsync();
            }
            bweather.RunWorkerAsync();
        }

And here are the event handlers:

// Weather handlers
            bweather.WorkerSupportsCancellation = true;
            bweather.DoWork += bweather_DoWork;
            bweather.RunWorkerCompleted += bweather_RunWorkerCompleted;

Any idea why is this not working as it should?

Thank you

user123_456
  • 5,635
  • 26
  • 84
  • 140

1 Answers1

2

Well the error message suggests that you're trying to use the same background worker more than once.

You're asking it to cancel if it's still busy, but that doesn't mean it'll cancel immediately. As far as I can tell, the BackgroundWorker code isn't even checking whether it's been cancelled, which means cancelling it won't really achieve anything useful.

I would suggest that if it's busy, you should instead just ignore the request. In fact, it might be better to disable the button completely when you start the operation, and only re-enable it when the operation completes.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • How can I check if BackgroundWorker has completed his request? – user123_456 Aug 18 '12 at 11:15
  • 1
    @denonth: You're already checking that with `IsBusy`. You just need to change what you do when it's busy. (Your logic is also the wrong way round - you're cancelling it if it *isn't* busy...) – Jon Skeet Aug 18 '12 at 11:30
  • This is what I have put inside `if (bweather.IsBusy) { weather_descripton.AppendText("Busy! Try later"); bweather.CancelAsync(); } else { weather_descripton.AppendText("Searching......."); bweather.RunWorkerAsync(); }` but now everytime I press it more then once I get error that `Operation has timeout`. Any idea that I can do next? – user123_456 Aug 18 '12 at 14:55
  • @denonth: That's presumably due to the cancellation. Why are you cancelling it? Why aren't you just disabling the button when you start it anyway? – Jon Skeet Aug 18 '12 at 17:54
  • I have tried what you said. But here is the problem. When I click once `bweather.isBusy()` is false and I am starting request `bweather.RunWorkerAsync();` when I get data I click once more and same thing again `bweather` is not busy and when it's start to fetch data then `timeout` occurs. This `isbusy()` method is working when I for example click button once and request is not done. So basically fetching data more then once is not working correctly. Please help – user123_456 Aug 28 '12 at 11:23
  • @denonth: Sorry, it's not really clear why anything's timing out... although if you're still cancelling the operation, that could explain things... – Jon Skeet Aug 28 '12 at 11:45
  • Jon I have been debugging for some time and I realized that I am getting exception in this line: `dow = WeatherLib.WProvider.Wunderground(latlong);` and error is `An exception occurred during a WebClient request.` any idea? Maybe I need to recreated class or something when doing each call? This instance of the class is made once at the beginning: `WeatherLib.DayOfWeek dow = new WeatherLib.DayOfWeek();` – user123_456 Aug 28 '12 at 15:36
  • @denonth: You should look at whether there's an `InnerException` which would give more information. – Jon Skeet Aug 28 '12 at 16:08
  • Hi Jon I have tried your `InnerException` suggestion and it really helped me a lot. Problem was in the picture that was fetched from the database and it was not disposed and it was giving the error all the time that is used by another program. Anyway thank you for your help – user123_456 Aug 30 '12 at 16:18