C # Timer i wyciek pamięci
Tworzę program, który będzie sprawdzał listę katalogów co 2 sekundy. Spodziewam się, że ten program będzie działał przez wiele miesięcy, nie przepuszczając pamięci ani nie wymagając interakcji z ludźmi.
Poniżej program ma wyciek pamięci.
Nadal nie jestem pewien, co reprezentuje 10K. To nie jest przerwa. Interwał wynosi 2k.
class Program
{
static void Main(string[] args)
{
Timer aTimer = new Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
GC.KeepAlive(aTimer);
}
private static void OnTimedEvent(object source, ElapsedEventArgs e )
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
string[] DirList = Directory.GetFiles(@"C:\TTImer");
if (DirList.Length > 0)
{
foreach (string s in DirList)
{
//do something
}
}
}
}