Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // why isn't the timer being disposed? see example session after code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Timers;
- class Program
- {
- static void Main()
- {
- StartTimer();
- Console.WriteLine("Timer started");
- while(true)
- {
- Console.ReadLine();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- Console.WriteLine("GC.Collect complete");
- }
- }
- static void StartTimer()
- {
- var timer = new Timer();
- timer.Interval = 1000;
- timer.Elapsed += (_1, _2) => Console.WriteLine("TICK");
- timer.Start();
- }
- }
- /*
- \> timer.exe
- Timer started
- TICK
- TICK
- TICK
- GC.Collect complete
- TICK
- TICK
- TICK
- GC.Collect complete
- TICK
- TICK
- TICK
- GC.Collect complete
- TICK
- TICK
- TICK
- TICK
- TICK
- TICK
- TICK
- TICK
- <Ctrl+C>*/
Advertisement
Add Comment
Please, Sign In to add comment