Date: 21.09.2018 15:52:49
Документация System.Timers.Timer, в отличие от System.Threading.Timer, не заявляет напрямую, что объект таймера подлежит уничтожению GC после освобождения всех пользовательских ссылок на него. Вы можете проверить это на практике с помощью такого кода:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTest
{
class Program
{
static void Test()
{
var timer1 = new System.Threading.Timer(
_ => Console.WriteLine("System.Threading.Timer callback"),
null,
0,
500);
var timer2 = new System.Timers.Timer
{
Interval = 500,
AutoReset = true
};
timer2.Elapsed += (_, __) => Console.WriteLine("System.Timers.Timer callback");
timer2.Enabled = true;
}
static void Main(string[] args)
{
Test();
System.Threading.Thread.Sleep(2000);
GC.Collect();
Console.WriteLine("GC collected");
Console.ReadKey();
}
}
}
Для .NET 4.5 тест показывает, что System.Threading.Timer уничтожается, а System.Timers.Timer - нет.Автор: VadimTagil