0
namespace LEDServer
{

    public class Weather
    {
        string weathervalue, weatherexplain, temperature;
        int temp;
        public void DoWork()
        {
            while (!_shouldStop)
            {
                try
                {
                    getWeather();
                }
                catch (Exception e)
                {
                }
                Thread.Sleep(5000);

            }
            Console.WriteLine("worker thread: terminating gracefully.");
        }
        public void RequestStop()
        {
            _shouldStop = true;
        }
        // Volatile is used as hint to the compiler that this data
        // member will be accessed by multiple threads.
        private volatile bool _shouldStop;

        public static void startgetWeather()
        {
            Weather weather = new Weather();
            Thread workerThread = new Thread(weather.DoWork);

            // Start the worker thread.
            workerThread.Start();
            Console.WriteLine("main thread: Starting worker thread...");

            // Loop until worker thread activates.

        }

        public void getWeather()
        {
            //weather data parsing
            string sURL;
            sURL = "http://api.openweathermap.org/data/2.5/weather?lat=37.56826&lon=126.977829&APPID=4cee5a3a82efa3608aaad71a754a94e0&mode=XML&units=metric" + "&dummy=" + Guid.NewGuid().ToString();

            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(sURL);
            myReq.Timeout = 10000;
            myReq.Method = "GET";
            StringBuilder output = new StringBuilder();
            HttpWebResponse wRes = (HttpWebResponse)myReq.GetResponse();
            Stream respGetStream = wRes.GetResponseStream();
            StreamReader readerGet = new StreamReader(respGetStream, Encoding.UTF8);
            XmlTextReader reader = new XmlTextReader(readerGet);

This program just work twice getWeather().

I don't know why this program just work twice..

And I made asyncsocket server and work together in Main Function.

But It doesn't work.. this problem is because of thread?

Each class is different..

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
  • 2
    As an aside, now would be a good time to learn about .NET naming conventions and start applying them. – Jon Skeet Nov 05 '15 at 16:51
  • Also, read up on `CancellationToken` - a cleaner solution than using volatile bool - your app as written might take 5 seconds to shutdown after a request to stop. – Ian Mercer Nov 05 '15 at 16:55
  • where you are setting RequestStop() ?!! I mean _shouldStop = true; – BRAHIM Kamel Nov 05 '15 at 16:55

1 Answers1

5

I don't know why this program just work twice..

Because the default HTTP connection pool has a size of 2 connections per host - and you're using them both up. You can configure this with the <connectionManagement> element in app.config, but you should only do this if you really need to.

Basically, by not disposing of the response, you're leaving the connection pool to the mercy of the garbage collector.

The fix is simple: dispose of resources appropriately:

using (var response = (HttpWebResponse)myReq.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        using (var reader = new XmlTextReader(responseStream))
        {
            // Whatever you need
        }
    }
}

Note that I've removed the creation of the InputStreamReader - the code I've presented here is more robust, as it will handle XML returned in different encodings; XmlTextReader will do the right thing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Nice catch, but where is the "2 connections per host" identified? Do you have a link? – Ron Beyer Nov 05 '15 at 17:00
  • @RonBeyer: I can't remember where I read about the default being 2 - but I've added a link for how it can be tweaked. – Jon Skeet Nov 05 '15 at 17:07
  • It is defined in your config file. Here is the MSDN: https://msdn.microsoft.com/en-us/library/fb6y0fyc.aspx As a note, @JonSkeet you mention this in one of your other posts I was able to dig up ( http://stackoverflow.com/questions/1361771/max-number-of-concurrent-httpwebrequests ). – gmiley Nov 05 '15 at 17:18