Results 1 to 8 of 8

Thread: [RESOLVED] Creating a Timer in code

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    37

    Resolved [RESOLVED] Creating a Timer in code

    I want to create a timer while the application is running. For example, the form has TextBox1 and Button1. When Button1 is clicked, the timer will be enabled and will print the current system time in TextBox1 every 10 seconds. The timer will be disabled when the Button1 is clicked again. How do I implement the timer and its events? An example with both Windows Forms and Silverlight would be great. Thanks!

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Creating a Timer in code

    From your description in the windows forms project you would not need to create anythign through code. You could simply add a timer to the project and set the enabled property to false. In your button code set enabled=true or false as needed.

    You would use the timers class to create a timer via code.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    37

    Re: Creating a Timer in code

    Quote Originally Posted by DataMiser View Post
    From your description in the windows forms project you would not need to create anythign through code. You could simply add a timer to the project and set the enabled property to false. In your button code set enabled=true or false as needed.
    I'm aware of that. However, my project is Silverlight/WP so I have to create the timer in code. I was just testing it out in Windows Forms before the WP Emulator crawls my system with every test.

    You would use the timers class to create a timer via code.
    Could you please give a short example, including the events? Thanks!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Creating a Timer in code

    There is only one event associated with a timer, which is the tick event. If you want a stub of a method to use in event handling, add a timer to the form, double click it to get the stub, then remove the timer from the form. This will leave a stub. Be sure that it has no Handles caluse at the end, because you don't want that.

    As for adding a timer, it is the same as adding any other form level variable:

    Private myTimer = New Windows.Forms.Timer

    The only difference is that you will have to hook the sub to the timer to handle the event. This is done with a line such as:

    AddHandler myTimer.Tick AddressOf myTimerEventHandler

    where myTimerEventHandler is the sub that will handle the event. You probably want to hook that handler up right away, but you can do it any time prior to starting the timer.
    My usual boring signature: Nothing

  5. #5
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 2011
    Posts
    147

    Re: Creating a Timer in code

    To create a timer from scratch, I suppose you could try making an infinite for loop and have it compare each second. For instance:
    vb Code:
    1. Dim iSec As Integer
    2.         Dim iChk As Integer = My.Computer.Clock.LocalTime.Second
    3.         For iSec = 0 To 2
    4.             If Not iChk = My.Computer.Clock.LocalTime.Second Then
    5.                 MsgBox("Worked")
    6.             End If
    7.             iSec = 0
    8.         Next

    WARNING: This totally froze my program. xD
    Last edited by EilaDoll; May 16th, 2012 at 04:33 PM. Reason: Total freeze.
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Creating a Timer in code

    That won't work. It would block all other activities on the form, so no other code will run unless it is running on a different thread, and even then the form would appear frozen since the events won't be pumped. You could solve that by adding an Application.DoEvents, but then you have the busy wait that is such a horrible solution to this type of problem. The CPU will utterly PEG while that is running, which will have a whole slew of undesirable consequences.
    My usual boring signature: Nothing

  7. #7
    Addicted Member EilaDoll's Avatar
    Join Date
    Dec 2011
    Posts
    147

    Re: Creating a Timer in code

    Yea, I noticed that. But it was fun to play with.
    http://www.vbforums.com/image.php?type=sigpic&userid=142423&dateline=1337150730

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    37

    Re: Creating a Timer in code

    While playing around with a Silverlight app, I discovered something that totally fits the bill:

    vb Code:
    1. Imports System.Windows.Threading
    2.  
    3. Dim WithEvents timer As DispatchTimer = New DispatchTimer
    4.  
    5. timer.Interval = New Timespan(0,0,1) ' Set the timer's interval
    6.  
    7. Private Sub TimerTick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick
    8.    ' Do work here.
    9. End Sub

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