How to stop a timer at a given time in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pavan Bhargav
    New Member
    • Feb 2012
    • 1

    How to stop a timer at a given time in C#?

    how to stop a timer at a given time? say 10mins?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Get the value of DateTime.Now before you start it and store it somewhere like a variable called startTime. Then check to see if (DateTime.Now - startTime).Tota lMinutes is greater than 10.

    (NOTE: Subtracting, or adding, two DateTime values results in a TimeSpan object)

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      I should also mention, if your timer is ticking every X seconds/minutes but you want to turn it off after exactly 10, you could always use a second timer. Create it, set the interval to whatever your timeout is, then turn both timers off when the second timer ticks.

      Whatever works best for you.

      Comment

      Working...