-
Oct 2nd, 2012, 03:47 PM
#1
Thread Starter
Junior Member
New Timer problem
I'm tryng to make a timer object but having no luck,
If i add timer1 to form1 and write:
me.text = "yeah"
Timer1.enabled = false
Then after one "tick" it will change the text, but to do the same thing with a timer object i've read that i need to write something like:
Public shared Tim as system.timers.timer
Public shared sub main()
Tim = new timers.timer(1000)
Tim.enabled = True
Addhandler Tim.elapsed, addressof TimTick
End sub
Private shared sub TimTick(sender as object, e as timers.elapsedeventargs)
Form1.text = "yeah"
Tim.enabled = false
End sub
This doesn't seem to work, am i missing something?
-
Oct 2nd, 2012, 04:01 PM
#2
Re: New Timer problem
I'm lost. If this is a Forms application why are you using sub Main()? It won't work if it's not executed.
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!
-
Oct 2nd, 2012, 04:02 PM
#3
Re: New Timer problem
I'm lost. If this is a Forms application why are you using sub Main()? It won't work if it's not executed.
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!
-
Oct 2nd, 2012, 04:17 PM
#4
Thread Starter
Junior Member
Re: New Timer problem
I spent an hour or so on the internet trying to find how to create a new timer and after the 1st thirty or so forums i found something that looked like this, and it looked like what i wanted.
If i open a new "windows form application", then what changes would i need to make to this to get it to work?
-
Oct 2nd, 2012, 05:47 PM
#5
Re: New Timer problem
vb.net Code:
Public Class Form1
Public Shared T As New System.Timers.Timer
Delegate Sub SetTextCallBack([text] As String) ' You're going to run into cross threading issues
' This is needed for a solution (see below)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
T.Interval = 2000
AddHandler T.Elapsed, AddressOf T_Tick
T.Enabled = True
End Sub
Private Sub SetText(ByVal [text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.InvokeRequired Then
Dim d As New SetTextCallBack(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.Text = [text]
End If
End Sub
Private Sub T_Tick(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs)
SetText("Ok")
T.Enabled = False
End Sub
End Class
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!
-
Oct 2nd, 2012, 06:10 PM
#6
Re: New Timer problem
Why are you using a System.Timers.Timer? Nomally, you would drag a timer onto the form. This would be a Forms.Timer, and they are quite a bit easier to use. The other timer is for threading, and you never said you wanted that. You can also create a Forms.Timer in code just like you can create anything else:
Public myTimer As Forms.Timer = New Forms.Timer
However, if you do that, you will also need to use AddHandler to hook the timer up to the method that you want to use to handle the tick. If you just drag the timer onto the form then you can just double click it and you will be taken to the method.
My usual boring signature: Nothing
-
Oct 2nd, 2012, 06:15 PM
#7
Re: New Timer problem
Originally Posted by Shaggy Hiker
Public myTimer As Forms.Timer = New Forms.Timer
However, if you do that, you will also need to use AddHandler to hook the timer up to the method that you want to use to handle the tick. If you just drag the timer onto the form then you can just double click it and you will be taken to the method.
A slightly easier alternative is to use the WithEvents keyword:
Public WithEvents myTimer As New Timer
Then you can code the Timer Tick sub the same way as if you added it in the Designer. It's what the VS Designer itself uses.
BB
-
Oct 2nd, 2012, 06:16 PM
#8
Re: New Timer problem
Ours is not to reason why ... or so they keep telling me. It was quite fun trying to work it all out with the cross thread and all so I don't feel the time (or should that be 'the timer'?) was wasted, (though I must confess to crushing a cake box in frustration at one point - serves me right for not reading the whole article I was consulting!)
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!
-
Oct 3rd, 2012, 04:45 AM
#9
Thread Starter
Junior Member
Re: New Timer problem
when I said I was trying to create a timer object I meant that I'd tried "Public Tim as new Timer" but found that I had no seperate "sub" for a "Tim.tick" meaning the timer was useless. So what your saying is that adding "WithEvents" would allow for a "Tim.Tick" Sub?
Thank you very much for the time and effort Dunfiddlin, I'll give that a go later.
-
Oct 3rd, 2012, 05:36 AM
#10
Re: New Timer problem
Originally Posted by sico
when I said I was trying to create a timer object I meant that I'd tried "Public Tim as new Timer" but found that I had no seperate "sub" for a "Tim.tick" meaning the timer was useless. So what your saying is that adding "WithEvents" would allow for a "Tim.Tick" Sub?.
Yes.
BB
-
Oct 3rd, 2012, 07:28 AM
#11
Re: New Timer problem
Originally Posted by sico
when I said I was trying to create a timer object I meant that I'd tried "Public Tim as new Timer" but found that I had no seperate "sub" for a "Tim.tick" meaning the timer was useless. So what your saying is that adding "WithEvents" would allow for a "Tim.Tick" Sub?
Thank you very much for the time and effort Dunfiddlin, I'll give that a go later.
As BB noted, yes, WithEvents will allow the timer 'Tick' event (note the difference between a forms timer and a system timer).
However, Adding a handler will do the same thing, similar to what you have done in your first post. However, your sub main() is executing and completing (thus ending the application) before any kind of event can fire.
Code:
Module Module1
Private Tim As System.Timers.Timer
Public Sub Main()
Tim = New Timers.Timer(1000)
Tim.Enabled = True
AddHandler Tim.Elapsed, AddressOf TimTick
Console.WriteLine("Find the ANY key")
Console.ReadKey(True)
End Sub
Private Sub TimTick(sender As Object, e As Timers.ElapsedEventArgs)
Console.WriteLine("Tim is Fired!")
Tim.Enabled = False
End Sub
End Module
Of course, this is a console application: if you want to play with forms, you'd create a forms application and typically use the Forms timer.
Last edited by SJWhiteley; Oct 3rd, 2012 at 07:30 AM.
Reason: speeling and clarification.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Oct 3rd, 2012, 09:18 AM
#12
Re: New Timer problem
Both AddHandler and WithEvents do the same thing, ultimately. When you have an oject that can raise events, such as controls, timers, custom objects with custom events, and so forth, the object is maintaining a list (one list per event) of methods that it needs to call whenever the event happens. WithEvents tells the object that will raise the event, that the current class wants to handle events. You then have to add the Handles clause to the end of the method that is supposed to handle the event (the designer does this for you for components added in the designer). The compiler will then hook the event to the handler. By comparison, AddHandler is a bit more straightforward. That method is used to tell the object that will raise the event, which method it should add to which list. No magic compiler hookups with AddHandler, it is a strange looking method, but one that works in a clear and understandable fashion once you know how the event raising mechanism works. WithEvents and the Handles clause is easier to use, simply because the designer hooks things up automatically, but how that happens isn't quite as clear.
My usual boring signature: Nothing
-
Oct 4th, 2012, 06:54 AM
#13
Thread Starter
Junior Member
Re: New Timer problem
Thanks for all the help and advice. I've got it all working now so i can carry on with my project.
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
|