Results 1 to 13 of 13

Thread: New Timer problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    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?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    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?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: New Timer problem

    vb.net Code:
    1. Public Class Form1
    2.     Public Shared T As New System.Timers.Timer
    3.     Delegate Sub SetTextCallBack([text] As String) ' You're going to run into cross threading issues
    4.                                                                     ' This is needed for a solution (see below)
    5.  
    6.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    7.         T.Interval = 2000
    8.         AddHandler T.Elapsed, AddressOf T_Tick
    9.         T.Enabled = True
    10.     End Sub
    11.  
    12.     Private Sub SetText(ByVal [text] As String)
    13.  
    14.         ' InvokeRequired required compares the thread ID of the
    15.         ' calling thread to the thread ID of the creating thread.
    16.         ' If these threads are different, it returns true.
    17.  
    18.         If Me.InvokeRequired Then
    19.             Dim d As New SetTextCallBack(AddressOf SetText)
    20.             Me.Invoke(d, New Object() {[text]})
    21.         Else
    22.             Me.Text = [text]
    23.         End If
    24.     End Sub
    25.  
    26.     Private Sub T_Tick(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs)
    27.         SetText("Ok")
    28.         T.Enabled = False
    29.     End Sub
    30. 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!

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

    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

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: New Timer problem

    Quote Originally Posted by Shaggy Hiker View Post
    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

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    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.

  10. #10
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: New Timer problem

    Quote Originally Posted by sico View Post
    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

  11. #11
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: New Timer problem

    Quote Originally Posted by sico View Post
    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."

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    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
  •  



Click Here to Expand Forum to Full Width