none
System.Threading.Timer Handle Leak? RRS feed

  • Frage

  • Hallo,

    I have the following code. This code creates a timer with an interval of 30 seconds. The code works just fine but if I look an the handles using perfmon (WinXP) I see that the handles used by the process is going always up. After 2 hours of test running the process, which do nearly nothing has over 800 handles and seems not to be going down. The funny thing is if I build this project for .Net 2.0 the behavior seems correct only under .Net 4.0 this handles leak comes up. Also was the effect away if I set the timer to 5 seconds instead of 30.

    Can anyone explain this.

    Snippet
    sing System;
    using System.Diagnostics;

    namespace TimerLeakApplication
    {
        class Program
        {
            static int Main(string[] args)
            {
                var timer = new System.Threading.Timer(new System.Threading.TimerCallback(timer_Elapsed));
                timer.Change(0, 30000);

                Console.ReadKey();

                return 0;
            }

            private static void timer_Elapsed(object args)
            {
                double count = 0;
                for (int i = 0; i < 1000; i++)
                {
                    count += Math.Sin(Convert.ToDouble(i));
                }
                Console.WriteLine(count);
            }
        }


    Freitag, 2. Dezember 2011 12:40