Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Updating weather using Threading

Updating weather using Threading

Scheduled Pinned Locked Moved C#
csharpquestionannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Etienne_123
    wrote on last edited by
    #1

    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;

    K L E 3 Replies Last reply
    0
    • E Etienne_123

      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;

      K Offline
      K Offline
      kstls
      wrote on last edited by
      #2

      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

      E 1 Reply Last reply
      0
      • K kstls

        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

        E Offline
        E Offline
        Etienne_123
        wrote on last edited by
        #3

        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??

        1 Reply Last reply
        0
        • E Etienne_123

          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;

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            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


            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • E Etienne_123

              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;

              E Offline
              E Offline
              Etienne_123
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Mycroft Holmes

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups