Results 1 to 3 of 3

Thread: [02/03] System.Timers.Timer and System.Windows.Forms.Timer

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    [02/03] System.Timers.Timer and System.Windows.Forms.Timer

    Just a bit of advice about timers.
    I have an event I want to fire aprox every minute, so have put this on an a windows.form timer with an interval of 60000.

    I then have other events I want to fire at specific times, so I put this on a System.Timers.Timer with the SynchronizingObject set to none.

    I then fire the events at the correct times like so

    VB Code:
    1. curmin = Minute(Now())
    2.         If curmin = 0 Then
    3.             curhour = Hour(Now())
    4.             If curhour = 0 Then
    5.                 curday = Weekday(Now())
    6.             End If
    7.         End If
    8.  
    9.  Select Case curhour
    10.             Case 8
    11.  
    12.                 If curmin = 1 Then
    13.                     deloldmes()
    14.                 End If
    15.  
    16.                 If curmin = 21 Then
    17.                     gettftp(1)
    18.                 End If
    19.  
    20.                 If curmin = 56 Then
    21.                     getttp(2)
    22.                 End If
    23.  
    24.  
    25.             Case 9
    26.                 If curmin = 1 Then
    27.                     getmonmes()
    28.                     deloldmes()
    29.                 End If
    30.             Case 10
    31. .
    32. .
    33. .
    34. end select

    I assume I have to be careful because in theory the two timers could fire at the same time so access the same procedures. Is this the only thing I need worry about and is this the correct way of doing what I'm doing?

    Cheers

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] System.Timers.Timer and System.Windows.Forms.Timer

    There's no reason that you have to use a Timers.Timer at all. You can just use Windows.Forms.Timers for everything and just Stop it in its Tick event. Alternatively you can set the SynchronizingObject of the Timers.Timer to the form and it will raise its Elapsed event in the UI thread, ensuring that its handlers cannot be executed at the same time as the Windows.Forms.Timer's Tick event handler.

    Having said that, there is nothing wrong with the way you're doing things but you need to recognise the fact that the Timers.Timer will raise its Elapsed event in a worker thread. In that case you need to use standard thread synchronisation techniques if there is any chance that they may interfere with each other.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: [02/03] System.Timers.Timer and System.Windows.Forms.Timer

    My worry is that I could lose a minute if I had 2 windows.form timer, timer 1 fires every second, timer 2 fires every minute. Is the below possible, and what's the best way around this


    timer2 fires at 12:20:59 seconds
    curmin = 20

    timer1 fires
    does some work

    timer 2 fires at 12:22:00
    curmin = 22

    So the event due to fire on the 21st minute never will

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