关于线程终止的问题
直接看代码吧!!
public class Main()
{
private static T1 m_t1;
private Thread m_thread;
public Main()
{
m_t1 = new T1();
m_thread= new Thread(new ThreadStart(m_t1.StartService));
}
public Start()
{
m_thread.Start();
}
public Stop()
{
m_thread.Abort();
//这样做好像不能终止线程,我应该怎么做呢?
}
}
public class T1
{
private Timer m_Timer = new Timer();
public void StartService()
{
this.m_Timer.Interval = 10000;
this.m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);
this.m_Timer.Start();
}
private void m_Timer_Elapsed(object sender, ElapsedEventArgs e)
{
//do something
}
}
------解决方案--------------------
C# code
public class T1
{
private Timer m_Timer = new Timer();
public Timer Timer
{
get
{
return m_Timer;
}
}
public void Stop()
{
m_Timer.Stop();
}
public void StartService()
{
this.m_Timer.Interval = 10000;
this.m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);
this.m_Timer.Start();
}
private void m_Timer_Elapsed(object sender, ElapsedEventArgs e)
{
//do something
}
}