做了一个定时抓天气的线程, 在全局类里启动.
一开始都好,但是不久(几个小时后)天气预报就不见了,线程莫名消失了
请教高手,为什么
线程程序
public WeatherThread()
{
ThreadStart thr_start_func = new ThreadStart(threadProc);
fThread = new Thread(thr_start_func);
fThread.Name = "getWeather";
/* fThread.IsBackground = true;
fThread.Start(); //starting the thread
flag = true;
*/
} public void trigger()
{
if (fThread.IsAlive)
{
fThread.Abort();
}
else
{
fThread.IsBackground = true;
fThread.Start();
}
} public static void threadProc()
{
while (true)
{
try
{
getRSS();
Thread.Sleep(timeinternal * 1000);
}
catch (Exception)
{
}
} }
}启动部分:global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
WeatherThread Wt = new WeatherThread();
Wt.trigger();
}
一开始都好,但是不久(几个小时后)天气预报就不见了,线程莫名消失了
请教高手,为什么
线程程序
public WeatherThread()
{
ThreadStart thr_start_func = new ThreadStart(threadProc);
fThread = new Thread(thr_start_func);
fThread.Name = "getWeather";
/* fThread.IsBackground = true;
fThread.Start(); //starting the thread
flag = true;
*/
} public void trigger()
{
if (fThread.IsAlive)
{
fThread.Abort();
}
else
{
fThread.IsBackground = true;
fThread.Start();
}
} public static void threadProc()
{
while (true)
{
try
{
getRSS();
Thread.Sleep(timeinternal * 1000);
}
catch (Exception)
{
}
} }
}启动部分:global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
WeatherThread Wt = new WeatherThread();
Wt.trigger();
}
因为你的线程是否继续往下运行, 要取决于getRSS这个方法的运行情况.
建议再启用一个线程作为监控线程. 当你的主任务线程多少时间没有响应了, 就应该用监控线程去处理掉.
可是就算getRSS方法出错,我也没有抛出异常啊, 他还是应该在while循环内啊
会继续调用getRSS方法啊