System.Timers.Timer是否在中止其工作线程时终止? [英] Does System.Timers.Timer terminates on aborting its working Thread?
本文介绍了System.Timers.Timer是否在中止其工作线程时终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这些在Thread
调用的方法上使用System.Timers.Timer
的代码:
I have these codes that use System.Timers.Timer
on a method that called by a Thread
:
private void timeWorker()
{
var timer = new Timer(1000);
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
public MyConstructor()
{
var thread=new Thread(timeWorker);
thread.IsBackground = true;
thread.Start();
//
thread.Abort();
}
System.Timers.Timer
是否在中止其工作线程时终止?
Does System.Timers.Timer
terminates on aborting its working Thread?
推荐答案
- 您不能通过中止线程杀死计时器.
-
Timer
有其自己的线程来调用事件处理程序. - 此外,正如@Damien_The_Unbeliever所说的,计时器不属于任何线程,它是系统对象.
-
GC
由于您的事件处理程序而不会收集计时器.
- You cannot kill timer by aborting thread.
Timer
has its own thread to invoke your event handler.- Also, as @Damien_The_Unbeliever said timer doesn't belong to any thread, it's system object.
GC
will not collect timer because of your event handler.
您应该在线程方法实现中使用try-catch来捕获ThreadAbortException
并停止计时器.
You should use try-catch in your thread method implementation, to catch ThreadAbortException
and stop timer.
这篇关于System.Timers.Timer是否在中止其工作线程时终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文