Ticks after Stop ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Ticks after Stop ?

  1. #1
    Join Date
    May 2010
    Location
    Montréal, Québec
    Posts
    9

    Ticks after Stop ?

    Hi,
    Does anyone know if pending Timer.Tick events (already in the message queue) are removed upon a Timer.Enabled = false or if they might still arrive after ?

    (talking about System.Windows.Forms.Timer here...)

    Thanks

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: Ticks after Stop ?

    If I understand you correctly, they should. You could also make use of the Stop method of the Timer. It is an interesting question, why do you ask ?

  3. #3
    Join Date
    May 2010
    Location
    Montréal, Québec
    Posts
    9

    Re: Ticks after Stop ?

    Well, the premice is simple : in a user event (a key down in my case), I stop a timer, and put back some members to null. From this point, I expect that no more tick events will occur (cause members are null and I don't want to check for that in the handler).

    So I was just reflecting on that and was wondering : if the timer elapses WHILE i am processing the key down message, just before the Timer.Stop() is executed, I believe a timer message will be posted in the message queue, waiting to call my handler.

    So I was wondering if Timer.Stop() takes care of that case. I was wondering if there is some sort of guaranty that this cannot happen.... And I could'nt find the anwser in Microsoft doc !!

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Ticks after Stop ?

    So, I have run into this same problem many times and have never quite understood its cause. It doesn't seem that it is an issue with queued messages, in my case the time never stopped at all after calling the "Stop()" method. So, I disconnect the event handler, call Stop, and then set the timer to null. This did the trick though it is really a hack and I would like to understand the cause if possible.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Ticks after Stop ?

    Well, I don't know if the Timer's behavior can be customized in this particular way, but the problem can be solved using a guard bool field - in conjunction with a guard condition, around the relevant code. Once what needed to be done is done, you change the value of this bool field, and from that point on the guarded code will not be executed even if the Timer does raise another Tick event.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Ticks after Stop ?

    Yup, looking back at my old code that is exactly what I did.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,090

    Re: Ticks after Stop ?

    That is odd. Sorry for misunderstanding, and thanks BigEd and Cthulu for clearing it up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center