Timer question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Naveen Mukkelli

    Timer question.

    Hi,

    Currently I'm using System.Threadin g.Timer to perform some tasks
    periodically,
    lets say every minute.

    How can we disable this timer(System.Th reading.Timer) so that the time spent
    in the callback method is not counted.

    Do we have an equivalent of
    System.Timers.T ime.Enabled
    property in System.Threadin g.Timer class?

    Kindly let me know.

    Cheers,
    Naveen.
  • wbekker

    #2
    Re: Timer question.

    Hi Naveen,

    To pauze the timer:

    timer.Change(Sy stem.Threading. Timeout.Infinit e,
    System.Threadin g.Timeout.Infin ite)

    You need to change it back to the original interval to start the timer
    again.

    Ward


    Naveen Mukkelli wrote:[color=blue]
    > Hi,
    >
    > Currently I'm using System.Threadin g.Timer to perform some tasks
    > periodically,
    > lets say every minute.
    >
    > How can we disable this timer(System.Th reading.Timer) so that the time spent
    > in the callback method is not counted.
    >
    > Do we have an equivalent of
    > System.Timers.T ime.Enabled
    > property in System.Threadin g.Timer class?
    >
    > Kindly let me know.
    >
    > Cheers,
    > Naveen.[/color]

    Comment

    • Bob Powell [MVP]

      #3
      Re: Timer question.

      You might find System.Timers.T imer is better suited to this task.

      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.





      "Naveen Mukkelli" <NaveenMukkelli @discussions.mi crosoft.com> wrote in
      message news:05B1C15B-CB6C-43A7-8BA2-84EA6C2936A6@mi crosoft.com...[color=blue]
      > Hi,
      >
      > Currently I'm using System.Threadin g.Timer to perform some tasks
      > periodically,
      > lets say every minute.
      >
      > How can we disable this timer(System.Th reading.Timer) so that the time
      > spent
      > in the callback method is not counted.
      >
      > Do we have an equivalent of
      > System.Timers.T ime.Enabled
      > property in System.Threadin g.Timer class?
      >
      > Kindly let me know.
      >
      > Cheers,
      > Naveen.[/color]


      Comment

      • Naveen Mukkelli

        #4
        Re: Timer question.

        Hi bob,

        but later on, I need to port the code to Compact Framework.
        I guess System.Timers.T imer is not supported in Compact Framework.

        Cheers,
        Naveen.


        "Bob Powell [MVP]" wrote:
        [color=blue]
        > You might find System.Timers.T imer is better suited to this task.
        >
        > --
        > Bob Powell [MVP]
        > Visual C#, System.Drawing
        >
        > Find great Windows Forms articles in Windows Forms Tips and Tricks
        > http://www.bobpowell.net/tipstricks.htm
        >
        > Answer those GDI+ questions with the GDI+ FAQ
        > http://www.bobpowell.net/faqmain.htm
        >
        > All new articles provide code in C# and VB.NET.
        > Subscribe to the RSS feeds provided and never miss a new article.
        >
        >
        >
        >
        >
        > "Naveen Mukkelli" <NaveenMukkelli @discussions.mi crosoft.com> wrote in
        > message news:05B1C15B-CB6C-43A7-8BA2-84EA6C2936A6@mi crosoft.com...[color=green]
        > > Hi,
        > >
        > > Currently I'm using System.Threadin g.Timer to perform some tasks
        > > periodically,
        > > lets say every minute.
        > >
        > > How can we disable this timer(System.Th reading.Timer) so that the time
        > > spent
        > > in the callback method is not counted.
        > >
        > > Do we have an equivalent of
        > > System.Timers.T ime.Enabled
        > > property in System.Threadin g.Timer class?
        > >
        > > Kindly let me know.
        > >
        > > Cheers,
        > > Naveen.[/color]
        >
        >
        >[/color]

        Comment

        • J.Marsch

          #5
          Re: Timer question.

          One thing that you could do would be to set a flag when you enter your timer
          handler. You will ignore any timer events while the flag is set, and reset
          it when you finish your handler:
          example:

          // warning, I'm writing this "off the cuff", so there might be syntax errors
          private bool TimerHandlerInP rogress;
          public void MyTimerCallback (object state)
          {
          lock(this)
          {
          if(this.TimerHa ndlerInProgress )
          return; // ignore the timer elapse-- we are still handling the
          last one
          else
          this.TimerHandl erInProgress = true;
          }

          try
          {
          // process your timer event here....
          }
          finally
          {
          // reset the handled flag
          lock(this)
          {
          this.TimerHandl erInProgress = false;
          }
          }
          }



          "Naveen Mukkelli" <NaveenMukkelli @discussions.mi crosoft.com> wrote in
          message news:05B1C15B-CB6C-43A7-8BA2-84EA6C2936A6@mi crosoft.com...[color=blue]
          > Hi,
          >
          > Currently I'm using System.Threadin g.Timer to perform some tasks
          > periodically,
          > lets say every minute.
          >
          > How can we disable this timer(System.Th reading.Timer) so that the time
          > spent
          > in the callback method is not counted.
          >
          > Do we have an equivalent of
          > System.Timers.T ime.Enabled
          > property in System.Threadin g.Timer class?
          >
          > Kindly let me know.
          >
          > Cheers,
          > Naveen.[/color]


          Comment

          Working...