Updating weather using Threading
-
Hi people I'm using a web service to display the current weather on my main interface. At the moment I have to click a button everytime I want to update the weather, and while this is updating, my interface stalls for however long it takes to update which is usually 5 seconds. What I want to know is, is there a way to have this running on a separate thread without clicking a button and have it update continuously while still being able to work with the main interface? Here is the code I use to update the current temperature:
string strTemperature = "";
GlobalWeatherService.GlobalWeatherSoapClient proxy = new aXYZ.GlobalWeatherService.GlobalWeatherSoapClient("GlobalWeatherSoap12", "http://www.webservicex.net/globalweather.asmx");
string weatherXml = proxy.GetWeather("Port Elizabeth", "South Africa");
StringReader reader = new StringReader(weatherXml);
XmlReader readerXml = XmlReader.Create(reader);
XmlDocument weatherXmlDoc = new XmlDocument();
weatherXmlDoc.Load(readerXml);for (int i = 0; i < weatherXmlDoc.ChildNodes.Count; i++)
{
for (int j = 0; j < weatherXmlDoc.ChildNodes[i].ChildNodes.Count; j++)
{
if (weatherXmlDoc.ChildNodes[i].ChildNodes[j].Name == "Temperature")
strTemperature = weatherXmlDoc.ChildNodes[i].ChildNodes[j].InnerText;
}
}lblTemperature.Content = strTemperature;
-
Hi people I'm using a web service to display the current weather on my main interface. At the moment I have to click a button everytime I want to update the weather, and while this is updating, my interface stalls for however long it takes to update which is usually 5 seconds. What I want to know is, is there a way to have this running on a separate thread without clicking a button and have it update continuously while still being able to work with the main interface? Here is the code I use to update the current temperature:
string strTemperature = "";
GlobalWeatherService.GlobalWeatherSoapClient proxy = new aXYZ.GlobalWeatherService.GlobalWeatherSoapClient("GlobalWeatherSoap12", "http://www.webservicex.net/globalweather.asmx");
string weatherXml = proxy.GetWeather("Port Elizabeth", "South Africa");
StringReader reader = new StringReader(weatherXml);
XmlReader readerXml = XmlReader.Create(reader);
XmlDocument weatherXmlDoc = new XmlDocument();
weatherXmlDoc.Load(readerXml);for (int i = 0; i < weatherXmlDoc.ChildNodes.Count; i++)
{
for (int j = 0; j < weatherXmlDoc.ChildNodes[i].ChildNodes.Count; j++)
{
if (weatherXmlDoc.ChildNodes[i].ChildNodes[j].Name == "Temperature")
strTemperature = weatherXmlDoc.ChildNodes[i].ChildNodes[j].InnerText;
}
}lblTemperature.Content = strTemperature;
You can indeed use a thread to handle this. Here's a nice beginners guide about threading here on Codeproject: Beginners Guide To Threading In .NET Part 1 of n
-
You can indeed use a thread to handle this. Here's a nice beginners guide about threading here on Codeproject: Beginners Guide To Threading In .NET Part 1 of n
Thanks. I'll go through that now. I tried using a DispatcherTimer like in the following code:
// Create a Timer with a Normal Priority DispatcherTimer \_timer = new DispatcherTimer(DispatcherPriority.Normal); // Set the Interval to 2 seconds \_timer.Interval = TimeSpan.FromMilliseconds(2000); // Set the callback to just show the time ticking away // NOTE: We are using a control so this has to run on // the UI thread \_timer.Tick += new EventHandler(delegate(object s, EventArgs a) { UpdateWeather(); }); // Start the timer \_timer.Start();
I've tried about 4 different ways of using the Dispatcher, but no matter what I try, my UI keeps freezing while it's doing UpdateWeather. Can it be because I call this Timer from my form_Loaded event??
-
Hi people I'm using a web service to display the current weather on my main interface. At the moment I have to click a button everytime I want to update the weather, and while this is updating, my interface stalls for however long it takes to update which is usually 5 seconds. What I want to know is, is there a way to have this running on a separate thread without clicking a button and have it update continuously while still being able to work with the main interface? Here is the code I use to update the current temperature:
string strTemperature = "";
GlobalWeatherService.GlobalWeatherSoapClient proxy = new aXYZ.GlobalWeatherService.GlobalWeatherSoapClient("GlobalWeatherSoap12", "http://www.webservicex.net/globalweather.asmx");
string weatherXml = proxy.GetWeather("Port Elizabeth", "South Africa");
StringReader reader = new StringReader(weatherXml);
XmlReader readerXml = XmlReader.Create(reader);
XmlDocument weatherXmlDoc = new XmlDocument();
weatherXmlDoc.Load(readerXml);for (int i = 0; i < weatherXmlDoc.ChildNodes.Count; i++)
{
for (int j = 0; j < weatherXmlDoc.ChildNodes[i].ChildNodes.Count; j++)
{
if (weatherXmlDoc.ChildNodes[i].ChildNodes[j].Name == "Temperature")
strTemperature = weatherXmlDoc.ChildNodes[i].ChildNodes[j].InnerText;
}
}lblTemperature.Content = strTemperature;
Hi, you probably need this[^]. BTW: does the weather really change every 5 seconds where you live? :)
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Hi, you probably need this[^]. BTW: does the weather really change every 5 seconds where you live? :)
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
I was going to lend him some of my threads if he can order me a cold snap for Tuesday.
Never underestimate the power of human stupidity RAH
-
Hi people I'm using a web service to display the current weather on my main interface. At the moment I have to click a button everytime I want to update the weather, and while this is updating, my interface stalls for however long it takes to update which is usually 5 seconds. What I want to know is, is there a way to have this running on a separate thread without clicking a button and have it update continuously while still being able to work with the main interface? Here is the code I use to update the current temperature:
string strTemperature = "";
GlobalWeatherService.GlobalWeatherSoapClient proxy = new aXYZ.GlobalWeatherService.GlobalWeatherSoapClient("GlobalWeatherSoap12", "http://www.webservicex.net/globalweather.asmx");
string weatherXml = proxy.GetWeather("Port Elizabeth", "South Africa");
StringReader reader = new StringReader(weatherXml);
XmlReader readerXml = XmlReader.Create(reader);
XmlDocument weatherXmlDoc = new XmlDocument();
weatherXmlDoc.Load(readerXml);for (int i = 0; i < weatherXmlDoc.ChildNodes.Count; i++)
{
for (int j = 0; j < weatherXmlDoc.ChildNodes[i].ChildNodes.Count; j++)
{
if (weatherXmlDoc.ChildNodes[i].ChildNodes[j].Name == "Temperature")
strTemperature = weatherXmlDoc.ChildNodes[i].ChildNodes[j].InnerText;
}
}lblTemperature.Content = strTemperature;
Okay it seems to work with the BackgroundWorker.
backgroundMessageGetter.DoWork += new DoWorkEventHandler(backgroundMessageGetter\_DoWork); backgroundMessageGetter.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundMessageGetter\_RunWorkerCompleted); backgroundMessageGetter.ProgressChanged += new ProgressChangedEventHandler(backgroundMessageGetter\_ProgressChanged); backgroundMessageGetter.WorkerReportsProgress = true; backgroundMessageGetter.RunWorkerAsync(this); void backgroundMessageGetter\_DoWork(object sender, DoWorkEventArgs e) { try { while (true) { GlobalWeatherService.GlobalWeatherSoapClient proxy = new aXYZ.GlobalWeatherService.GlobalWeatherSoapClient("GlobalWeatherSoap12", "http://www.webservicex.net/globalweather.asmx"); string weatherXml = proxy.GetWeather("Port Elizabeth", "South Africa"); //some other processing backgroundMessageGetter.ReportProgress(0); Thread.Sleep(2000); } } catch (Exception error) { System.Windows.MessageBox.Show(error.Message); } } void backgroundMessageGetter\_ProgressChanged(object sender, ProgressChangedEventArgs e) { //update UI }
modified on Saturday, November 14, 2009 8:42 AM
-
I was going to lend him some of my threads if he can order me a cold snap for Tuesday.
Never underestimate the power of human stupidity RAH
No problem. Is a 5-second cold snap within an otherwise hot November day sufficient? :)
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages