private void StopQueryDataThread()
{
// 终止数据查询线程
m_stopQuery = true;
if (m_dataQueryThread != null)
{
m_dataQueryThread.Join(300);
if ((m_dataQueryThread != null) && (m_dataQueryThread.IsAlive))
{
m_dataQueryThread.Abort();
}
m_dataQueryThread = null;
}
}
private bool StartMonitorConnectionThread()
{
try
{
m_connectionMonitorThread = new Thread(new ThreadStart(MonitorConnectionThreadStart));
m_connectionMonitorThread.Priority = ThreadPriority.Lowest;
m_connectionMonitorThread.Name = "连接监视线程";
m_connectionMonitorThread.Start();
m_stopMonitor = false;
}
catch (ThreadStateException) // 线程已启动
{
return true;
}
catch (SecurityException) // 调用方没有适当的 SecurityPermission
{
return false;
}
catch (OutOfMemoryException) // 没有足够的内存启动该线程
{
return false;
} return true;
}
第一次调用StartMonitorThread时线程正常运行,线程运行后我在另一个优先级较低的线程中调用StopQueryDataThread()后再调用StartMonitorConnectionThread()出现问题,try语句捕获不到异常,调试出现以下信息,线程状态为Stopped,请问是怎么回事?调试错误提示为:“dataQueryThread.Priority”引发了“System.Threading.ThreadStateException”类型的异常 System.Threading.ThreadPriority {System.Threading.ThreadStateException}“dataQueryThread.IsBackground”引发了“System.Threading.ThreadStateException”类型的异常
谢谢yilanwuyu123,我试一下
问题莫名其妙,在最初的设计中也是这么实现的,断开后是可以重新连接的,把这个类重写之后发现在线程中启动线程,启动的线程不能运行且捕获不到任何异常