Results 1 to 5 of 5

Thread: Self-toggling timer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    122

    Self-toggling timer

    Hi ! I need help with this, because i cant think anything that would do this.
    I have a timer with interval of 1ms and it does some work, and now i want to make it:
    Turn on and do the work for 500ms (no not the interval 500ms, but the timer has to be toggled for 500 milliseconds) and after those 500 ms it has to turn off for next 500ms, then it turns on back etc.So its like:
    Turn on -> do the work -> after 500 ms turn off (.Enabled = False) -> stay turned off for 500 ms -> Turn on..

    How can i do it ? I cant think anything

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Self-toggling timer

    use 2 timers. The first timer is set to your 1ms interval and the second set to 500ms. In the tick event of the second timer toggle the enabled bit of the first timer using Timer.enabled = Not Timer1.Enabled
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Self-toggling timer

    I have a timer with interval of 1ms
    No. you have a timer set for an interval of 1ms. The chances that it's actually firing at 1ms intervals is absolutely zero. Microsoft recommends a minimum interval of 35ms. most people 50ms and me 100ms. At best you'll get inconsistent performance, at worst you'll crash the event queue. Either way it's really not worth it. If you really need something to happen 1000 times a second (and that's extremely unlikely) then you should use one of the other VB timer classes.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,380

    Re: Self-toggling timer

    Quote Originally Posted by dunfiddlin View Post
    No. you have a timer set for an interval of 1ms. The chances that it's actually firing at 1ms intervals is absolutely zero. Microsoft recommends a minimum interval of 35ms. most people 50ms and me 100ms. At best you'll get inconsistent performance, at worst you'll crash the event queue. Either way it's really not worth it. If you really need something to happen 1000 times a second (and that's extremely unlikely) then you should use one of the other VB timer classes.
    A good example of this is a game loop. Generally new programmers use a Timer and set it's interval to really low(like 1ms), the problem with that is just like dunfiddlin described; the event queue gets all jacked up. Also while Kebo's suggestion may work, the more timers there are running at the same time, the worse they get. So my point I'm trying to get across is, you should probably look into redesigning what you're trying to accomplish. Why don't you tell us what you're trying to accomplish, there maybe a better way of doing what you're trying.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Self-toggling timer

    The question isn't clear. If "do the work" isn't UI intensive I'd just get rid of the timer altogether and use a separate thread with appropriate sleeps.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

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



Click Here to Expand Forum to Full Width