天气取文不会多次使用

时间:2012-08-18 11:06:02

标签: c# webclient weather wunderground

我在使用Wunderground预测时遇到问题我正在使用它来检索c#程序中的数据。

当我一切正常工作后点击检索数据但当我再次点击按钮时,我收到此错误: Background Worker

这是我的代码:

        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);
            }
        }

这是刷新按钮:

 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();
        }

以下是事件处理程序:

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

任何想法为什么这不起作用?

谢谢

1 个答案:

答案 0 :(得分:2)

错误消息表明您尝试多次使用同一个后台工作程序。

如果它仍然很忙,你会要求它取消,但这并不意味着它会立即取消。据我所知,BackgroundWorker代码甚至没有检查它是否被取消,这意味着取消它不会真正实现任何有用的东西。

我建议如果它很忙,你应该忽略该请求。事实上,当启动操作时,最好完全禁用按钮,并且只在操作完成时重新启用它。