C# get 和 Thread class 只是工作了两次,具体我也不清楚
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);
这个程序只运行两次getWeather()
。
我不知道为什么这个程序只能运行两次..
和我制作了asyncsocket服务器,在Main Function中一起工作。
但是不行..这个问题是因为线程吗?
每个class都不一样..
I don't know why this program just work twice..
因为默认 HTTP 连接池的大小为每个主机 2 个连接 - 而您正在使用它们。您 可以 使用 app.config
中的 <connectionManagement>
元素配置它,但只有在确实需要时才应该这样做。
基本上,如果不处理响应,您就将连接池留给了垃圾收集器。
解决方法很简单:适当地处理资源:
using (var response = (HttpWebResponse)myReq.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
using (var reader = new XmlTextReader(responseStream))
{
// Whatever you need
}
}
}
请注意,我已经删除了 InputStreamReader
的创建 - 我在此处提供的代码更加健壮,因为它将处理以不同编码返回的 XML; XmlTextReader
会做正确的事。
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);
这个程序只运行两次getWeather()
。
我不知道为什么这个程序只能运行两次..
和我制作了asyncsocket服务器,在Main Function中一起工作。
但是不行..这个问题是因为线程吗?
每个class都不一样..
I don't know why this program just work twice..
因为默认 HTTP 连接池的大小为每个主机 2 个连接 - 而您正在使用它们。您 可以 使用 app.config
中的 <connectionManagement>
元素配置它,但只有在确实需要时才应该这样做。
基本上,如果不处理响应,您就将连接池留给了垃圾收集器。
解决方法很简单:适当地处理资源:
using (var response = (HttpWebResponse)myReq.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
using (var reader = new XmlTextReader(responseStream))
{
// Whatever you need
}
}
}
请注意,我已经删除了 InputStreamReader
的创建 - 我在此处提供的代码更加健壮,因为它将处理以不同编码返回的 XML; XmlTextReader
会做正确的事。