System.Threading.Timer improper function in Release mode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hagay Lupesko

    System.Threading.Timer improper function in Release mode

    Hi,

    I've encountered a strange phenomena which appears to me as a bug:

    I have an engine that uses a System.Threadin g.Timer to invoke a delegate
    every X minutes.

    The code looks something like this:
    TimerCallback callBack = new TimerCallback(R un); // run is obviously a
    method
    analysisTimer = new Timer(callBack, null, 0, delay);
    Console.ReadLin e();

    Everything works just fine when the code is build in Debug mode, but when
    the code is build in Release mode the delegate is invoked once only.

    After creating debug symbols for the Release mode it looks like the timer
    class is optimized away...

    The workaround solution I used was declaring the timer as static.

    But obviously it's not very nice...

    Any clue as to is this a bug, or what's wrong with the code ?

    Thanks, Hagay.


  • Vijaye Raji

    #2
    Re: System.Threadin g.Timer improper function in Release mode

    You say that the Run method is called once in release mode. If it was
    optimized away, it shouldn't be called even once.

    It's hard to tell why the code was optimized away from the snippet you
    provided. Is this a console/windows app? If Console, what is your current
    thread doing? What is the code inside the Run method?

    -vJ

    "Hagay Lupesko" <hagayl@nice.co m> wrote in message
    news:usMMJWzNEH A.2780@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi,
    >
    > I've encountered a strange phenomena which appears to me as a bug:
    >
    > I have an engine that uses a System.Threadin g.Timer to invoke a delegate
    > every X minutes.
    >
    > The code looks something like this:
    > TimerCallback callBack = new TimerCallback(R un); // run is obviously a
    > method
    > analysisTimer = new Timer(callBack, null, 0, delay);
    > Console.ReadLin e();
    >
    > Everything works just fine when the code is build in Debug mode, but when
    > the code is build in Release mode the delegate is invoked once only.
    >
    > After creating debug symbols for the Release mode it looks like the timer
    > class is optimized away...
    >
    > The workaround solution I used was declaring the timer as static.
    >
    > But obviously it's not very nice...
    >
    > Any clue as to is this a bug, or what's wrong with the code ?
    >
    > Thanks, Hagay.
    >
    >[/color]


    Comment

    • Hagay Lupesko

      #3
      Re: System.Threadin g.Timer improper function in Release mode

      Hi vj,

      I can say the timer was optimized away pretty certainly, because when
      building the app in release mode with optimization switch turned off, the
      app ran as required.

      Anyway, in response to your question, it's a console app, and as you cann
      see in the code the current thread is blocking on Console.Readlin e() .

      Hagay.

      "Vijaye Raji" <no_spam_vganes h@hotmail.com> wrote in message
      news:erb7QlzNEH A.2708@TK2MSFTN GP11.phx.gbl...[color=blue]
      > You say that the Run method is called once in release mode. If it was
      > optimized away, it shouldn't be called even once.
      >
      > It's hard to tell why the code was optimized away from the snippet you
      > provided. Is this a console/windows app? If Console, what is your[/color]
      current[color=blue]
      > thread doing? What is the code inside the Run method?
      >
      > -vJ
      >
      > "Hagay Lupesko" <hagayl@nice.co m> wrote in message
      > news:usMMJWzNEH A.2780@TK2MSFTN GP09.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I've encountered a strange phenomena which appears to me as a bug:
      > >
      > > I have an engine that uses a System.Threadin g.Timer to invoke a delegate
      > > every X minutes.
      > >
      > > The code looks something like this:
      > > TimerCallback callBack = new TimerCallback(R un); // run is obviously a
      > > method
      > > analysisTimer = new Timer(callBack, null, 0, delay);
      > > Console.ReadLin e();
      > >
      > > Everything works just fine when the code is build in Debug mode, but[/color][/color]
      when[color=blue][color=green]
      > > the code is build in Release mode the delegate is invoked once only.
      > >
      > > After creating debug symbols for the Release mode it looks like the[/color][/color]
      timer[color=blue][color=green]
      > > class is optimized away...
      > >
      > > The workaround solution I used was declaring the timer as static.
      > >
      > > But obviously it's not very nice...
      > >
      > > Any clue as to is this a bug, or what's wrong with the code ?
      > >
      > > Thanks, Hagay.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: System.Threadin g.Timer improper function in Release mode

        Hagay Lupesko <hagayl@nice.co m> wrote:[color=blue]
        > I can say the timer was optimized away pretty certainly, because when
        > building the app in release mode with optimization switch turned off, the
        > app ran as required.
        >
        > Anyway, in response to your question, it's a console app, and as you cann
        > see in the code the current thread is blocking on Console.Readlin e() .[/color]

        Could you post a short but complete program which demonstrates the
        problem?

        See http://www.pobox.com/~skeet/csharp/complete.html for details of
        what I mean by that.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Vijaye Raji

          #5
          Re: System.Threadin g.Timer improper function in Release mode

          I wrote a tiny app as below and I can't repro the problem. Do you see any
          differences between this app and the one you have?

          class Class1
          {
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main(string[] args)
          {
          System.Threadin g.Timer t = new System.Threadin g.Timer(
          new System.Threadin g.TimerCallback (OnTimer), null, 0, 1000);
          Console.ReadLin e();
          }

          static void OnTimer(object obj)
          {
          Console.WriteLi ne("Yes");
          }
          }

          "Hagay Lupesko" <hagayl@nice.co m> wrote in message
          news:%233K0BS0N EHA.1608@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > Hi vj,
          >
          > I can say the timer was optimized away pretty certainly, because when
          > building the app in release mode with optimization switch turned off, the
          > app ran as required.
          >
          > Anyway, in response to your question, it's a console app, and as you cann
          > see in the code the current thread is blocking on Console.Readlin e() .
          >
          > Hagay.
          >
          > "Vijaye Raji" <no_spam_vganes h@hotmail.com> wrote in message
          > news:erb7QlzNEH A.2708@TK2MSFTN GP11.phx.gbl...[color=green]
          >> You say that the Run method is called once in release mode. If it was
          >> optimized away, it shouldn't be called even once.
          >>
          >> It's hard to tell why the code was optimized away from the snippet you
          >> provided. Is this a console/windows app? If Console, what is your[/color]
          > current[color=green]
          >> thread doing? What is the code inside the Run method?
          >>
          >> -vJ
          >>
          >> "Hagay Lupesko" <hagayl@nice.co m> wrote in message
          >> news:usMMJWzNEH A.2780@TK2MSFTN GP09.phx.gbl...[color=darkred]
          >> > Hi,
          >> >
          >> > I've encountered a strange phenomena which appears to me as a bug:
          >> >
          >> > I have an engine that uses a System.Threadin g.Timer to invoke a
          >> > delegate
          >> > every X minutes.
          >> >
          >> > The code looks something like this:
          >> > TimerCallback callBack = new TimerCallback(R un); // run is obviously a
          >> > method
          >> > analysisTimer = new Timer(callBack, null, 0, delay);
          >> > Console.ReadLin e();
          >> >
          >> > Everything works just fine when the code is build in Debug mode, but[/color][/color]
          > when[color=green][color=darkred]
          >> > the code is build in Release mode the delegate is invoked once only.
          >> >
          >> > After creating debug symbols for the Release mode it looks like the[/color][/color]
          > timer[color=green][color=darkred]
          >> > class is optimized away...
          >> >
          >> > The workaround solution I used was declaring the timer as static.
          >> >
          >> > But obviously it's not very nice...
          >> >
          >> > Any clue as to is this a bug, or what's wrong with the code ?
          >> >
          >> > Thanks, Hagay.
          >> >
          >> >[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          Working...