|
-
May 16th, 2012, 09:46 AM
#1
Thread Starter
Member
[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!
-
May 16th, 2012, 09:51 AM
#2
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.
-
May 16th, 2012, 09:52 AM
#3
Thread Starter
Member
Re: Creating a Timer in code
 Originally Posted by DataMiser
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!
-
May 16th, 2012, 11:48 AM
#4
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
 
-
May 16th, 2012, 04:03 PM
#5
Addicted Member
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:
Dim iSec As Integer Dim iChk As Integer = My.Computer.Clock.LocalTime.Second For iSec = 0 To 2 If Not iChk = My.Computer.Clock.LocalTime.Second Then MsgBox("Worked") End If iSec = 0 Next
WARNING: This totally froze my program. xD
Last edited by EilaDoll; May 16th, 2012 at 04:33 PM.
Reason: Total freeze.
-
May 16th, 2012, 04:31 PM
#6
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
 
-
May 16th, 2012, 04:35 PM
#7
Addicted Member
Re: Creating a Timer in code
Yea, I noticed that. But it was fun to play with.
-
May 16th, 2012, 08:39 PM
#8
Thread Starter
Member
Re: Creating a Timer in code
While playing around with a Silverlight app, I discovered something that totally fits the bill:
vb Code:
Imports System.Windows.Threading Dim WithEvents timer As DispatchTimer = New DispatchTimer timer.Interval = New Timespan(0,0,1) ' Set the timer's interval Private Sub TimerTick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick ' Do work here. 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|