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();
}
Does System.Timers.Timer
terminates on aborting its working Thread?
MSDN C# Threading
I suggest this link for general working with threads. It is also mentioned that
Abort
will interrupt the Thread and prevents used ressources to be cleaned up.A better option would be a method to call that disables the while-loop inside the
timeWorker
Class likewhile the worker is doing his work in the loop
with a variable
private volatile bool running
.Timer
has its own thread to invoke your event handler.GC
will not collect timer because of your event handler.You should use try-catch in your thread method implementation, to catch
ThreadAbortException
and stop timer.