C# get and Thread class just work twice, I don't know exactly

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

Jon Skeet
people
quotationmark

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.

people

See more on this question at Stackoverflow