Question

Locked

vb.net timer control woes

By hermiodinator ·
Hi guys, this is my first post so please be gentle

I`m having problems with the timer control that I hope people can help me out with.

Basically, i`m creating the timer control at runtime (full code at end of post) and it just will not work and I don't know why!

I've compared it to code used to control a design-time timer and they are practically identical (bar the timer generation code obviously). I've looked at some tutorials for creating timers at runtime and my code seems almost identical to theirs, I don't know what I`m doing wrong!

Here's the code:
inherits System.Windows.Forms.form

Dim tTimer As System.Timers.Timer

Sub Form2Load(ByVal sender As Object, ByVal e As EventArgs)

tTimer=New System.Timers.Timer
ttimer.interval=1000
tTimer.Enabled=false
AddHandler tTimer.elapsed, AddressOf TimerFired

End Sub

Private Sub TimerFired(ByVal sender As Object, _
ByVal e As System.Timers.ElapsedEventArgs)

label1.Text = now.ToLongTimeString.tostring()
End
Sub

Sub Button1Click(ByVal sender As Object, ByVal e As EventArgs)
msgbox(tTimer.interval)
tTimer.Enabled=true
End Sub

Hope someone can help before I damage the computer!

This conversation is currently closed to new comments.

6 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

The only think I can think of is scoping

by Tony Hopkinson In reply to vb.net timer control woes

Try it this way without the dim.

http://abstractvb.com/code.asp?F=1&P=1&A=1032

Collapse -

got it

by hermiodinator In reply to The only think I can thin ...

thanks for your reply Tony.

I got a working reply from another forum where I posted this problem.

All that is needed is to use the windows.forms.timer class instead of the system.timers.timer class and the code works fine.

Would never have believed it would be that simple. I didn't even realise there was more than one timer class in .net!

Collapse -

Hmm, somthing funny going on here

by Tony Hopkinson In reply to got it

Found this

http://msdn2.microsoft.com/en-us/library/tb9yt5e6(VS.71).aspx

Going to have an experiment tomorrow, not seeing anything that tells me why the code shouldn't have worked, short of system.timer not being passing the elapsed event to the foreground message queue, synchronisation maybe ?
Hmmm frankly puzzled.


Nice to know the distinction has such an impact though.

Cheers, serious dandruff creator that one if you aren't au fait.

Collapse -

Aha , threading it is

by Tony Hopkinson In reply to Hmm, somthing funny going ...

Try update a windows control from a system.timer and in VS2005 you get told off because the timer is in a diferent thread.

For the curious, here's the help link on the exception

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm

Collapse -

Edited by moderator

by kylej In reply to vb.net timer control woes

Edited

Message was edited by: beth.blakely@...

Back to Windows Forum
6 total posts (Page 1 of 1)  

Related Discussions

Related Forums