Getting a timer to work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hotrod2000

    #1

    Getting a timer to work

    I'm quite new to programming but I'm having problems getting a timer
    to work in visual studio.net

    I've created a timer on a form, enabled it and then typed the
    following code (from the mdsn library as I thought this would be a
    good start!!!) but nothing happens :-


    Imports System.Timers


    Public Class Form1
    Inherits System.Windows. Forms.Form


    #Region " Windows Form Designer generated code "


    Public Sub New()
    MyBase.New()


    InitializeCompo nent()


    End Sub


    Protected Overloads Overrides Sub Dispose(ByVal disposing As
    Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Disp ose()
    End If
    End If
    MyBase.Dispose( disposing)
    End Sub


    Private components As System.Componen tModel.IContain er


    'NOTE: The following procedure is required by the Windows Form
    Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents Timer1 As System.Windows. Forms.Timer
    <System.Diagnos tics.DebuggerSt epThrough()Priv ate Sub
    InitializeCompo nent()
    Me.components = New System.Componen tModel.Containe r
    Dim configurationAp pSettings As
    System.Configur ation.AppSettin gsReader = New
    System.Configur ation.AppSettin gsReader
    Me.Timer1 = New System.Windows. Forms.Timer(Me. components)
    '
    'Timer1
    '
    Me.Timer1.Enabl ed =
    CType(configura tionAppSettings .GetValue("Time r1.Enabled",
    GetType(System. Boolean)), Boolean)
    Me.Timer1.Inter val =
    CType(configura tionAppSettings .GetValue("Time r1.Interval",
    GetType(System. Int32)), Integer)
    '
    'Form1
    '
    Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
    Me.ClientSize = New System.Drawing. Size(292, 266)
    Me.Name = "Form1"
    Me.Text = "Form1"


    End Sub


    #End Region


    End Class


    Public Class Timer1


    Public Shared Sub Main()


    Dim aTimer As New System.Timers.T imer


    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent


    aTimer.Interval = 2000
    aTimer.Enabled = True


    Console.WriteLi ne("Press the Enter key to exit the program.")
    Console.ReadLin e()


    GC.KeepAlive(aT imer)
    End Sub


    Private Shared Sub OnTimedEvent(By Val source As Object, ByVal e
    As
    ElapsedEventArg s)
    Console.WriteLi ne("Hello World!")


    End Sub
    End Class


    Any help would be greatly appreciated.


    Thanks
    Paul

  • Armin Zingler

    #2
    Re: Getting a timer to work

    "Hotrod2000 " <pbrunyee@bmsel land.comschrieb
    I'm quite new to programming but I'm having problems getting a timer
    to work in visual studio.net
    >
    I've created a timer on a form, enabled it and then typed the
    following code (from the mdsn library as I thought this would be a
    good start!!!) but nothing happens :-

    - Did you set the sub main below as the startup object?
    - The Timer in the Form has no purpose because the Form is never shown.
    - Is the project type "console application"? Othwerwise you don't see the
    output in the console (because you don't see the console).


    Armin

    Public Class Timer1
    >
    >
    Public Shared Sub Main()
    >
    >
    Dim aTimer As New System.Timers.T imer
    >
    >
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
    >
    >
    aTimer.Interval = 2000
    aTimer.Enabled = True
    >
    >
    Console.WriteLi ne("Press the Enter key to exit the program.")
    Console.ReadLin e()
    >
    >
    GC.KeepAlive(aT imer)
    End Sub
    >
    >
    Private Shared Sub OnTimedEvent(By Val source As Object, ByVal e
    As
    ElapsedEventArg s)
    Console.WriteLi ne("Hello World!")
    >
    >
    End Sub
    End Class
    >

    Comment

    • Matt F

      #3
      Re: Getting a timer to work

      From your code, it looks like you're attempting to attach to a Elapsed
      event, which doesn't exist.

      Try changing the line:
      AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

      To:
      AddHandler aTimer.Tick, AddressOf OnTimedEvent

      I'm surprised the VS will even compile the project without errors. Out of
      curiosity, do you have the msdn link that you mentioned?


      Comment

      • Armin Zingler

        #4
        Re: Getting a timer to work

        "Matt F" <mfielderREMOVE CAPS@nospam.nos pamschrieb
        From your code, it looks like you're attempting to attach to a
        Elapsed event, which doesn't exist.
        >
        Try changing the line:
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        >
        To:
        AddHandler aTimer.Tick, AddressOf OnTimedEvent
        >
        I'm surprised the VS will even compile the project without errors.
        Out of curiosity, do you have the msdn link that you mentioned?
        >
        The type of aTimer is System.Timers.T imer which does have an Elapsed event.


        Armin

        Comment

        • Matt F

          #5
          Re: Getting a timer to work

          your right --- sorry for the post --- this is a good examply of why I
          shouldn't be posting stuff at 2:20 AM


          Comment

          • Armin Zingler

            #6
            Re: Getting a timer to work

            "Matt F" <mfielderREMOVE CAPS@nospam.nos pamschrieb
            your right --- sorry for the post --- this is a good examply of why
            I shouldn't be posting stuff at 2:20 AM
            But it was 11:20 AM! ;-)


            Armin

            Comment

            • Matt F

              #7
              Re: Getting a timer to work

              LOL ---

              So back to the original question --- did the original poster ever get his
              issue resolved?


              Comment

              • Armin Zingler

                #8
                Re: Getting a timer to work

                "Matt F" <mfielderREMOVE CAPS@nospam.nos pamschrieb
                LOL ---
                >
                So back to the original question --- did the original poster ever
                get his issue resolved?

                Wait til 11:20 tomorrow - or til the Timer ticks. :-D


                Armin

                Comment

                • Hotrod2000

                  #9
                  Re: Getting a timer to work

                  On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                  "Matt F" <mfielderREMOVE C...@nospam.nos pamschrieb
                  >
                  LOL ---
                  >
                  So back to the original question --- did the original poster ever
                  get his issue resolved?
                  >
                  Wait til 11:20 tomorrow - or til the Timer ticks. :-D
                  >
                  Armin
                  Thanks for all your posts...
                  I'm still struggling though...
                  All I'm trying to do is open a form with a few buttons on, (which
                  action different events, not a problem with that) but at the same time
                  start a timer which puts a message up if no button is pressed, but if
                  one of the buttons are pressed, the timer is reset.
                  I was hoping that by using a timer on the form I would just be able to
                  call it start and stop it when an action was performed.
                  Am I even using the correct timer ? as I understand there are three
                  different timers, windows.forms.t imers.timer, system.timers and a
                  threading timer...
                  Having looked at what I'm doing I seem to using a mixture !!!(Not by
                  design) of the windows.form.ti mers.timer and the
                  system.timers.t imer !!!
                  Where do I actually put the code for this ? is it on the form, or is
                  it in it's own timer class ?

                  Comment

                  • Hotrod2000

                    #10
                    Re: Getting a timer to work

                    On Aug 23, 1:33 pm, Hotrod2000 <pbrun...@bmsel land.comwrote:
                    On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                    >
                    "Matt F" <mfielderREMOVE C...@nospam.nos pamschrieb
                    >
                    LOL ---
                    >
                    So back to the original question --- did the original poster ever
                    get his issue resolved?
                    >
                    Wait til 11:20 tomorrow - or til the Timer ticks. :-D
                    >
                    Armin
                    >
                    Thanks for all your posts...
                    I'm still struggling though...
                    All I'm trying to do is open a form with a few buttons on, (which
                    action different events, not a problem with that) but at the same time
                    start a timer which puts a message up if no button is pressed, but if
                    one of the buttons are pressed, the timer is reset.
                    I was hoping that by using a timer on the form I would just be able to
                    call it start and stop it when an action was performed.
                    Am I even using the correct timer ? as I understand there are three
                    different timers, windows.forms.t imers.timer, system.timers and a
                    threading timer...
                    Having looked at what I'm doing I seem to using a mixture !!!(Not by
                    design) of the windows.form.ti mers.timer and the
                    system.timers.t imer !!!
                    Where do I actually put the code for this ? is it on the form, or is
                    it in it's own timer class ?
                    P.S. the link to the msdn page I got my original code from is :-
                    http://msdn2.microsoft.com/en-us/lib...er(vs.71).aspx

                    Thanks
                    Paul

                    Comment

                    • Armin Zingler

                      #11
                      Re: Getting a timer to work

                      "Hotrod2000 " <pbrunyee@bmsel land.comschrieb
                      On Aug 23, 1:33 pm, Hotrod2000 <pbrun...@bmsel land.comwrote:
                      On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                      "Matt F" <mfielderREMOVE C...@nospam.nos pamschrieb
                      LOL ---
                      So back to the original question --- did the original poster
                      ever get his issue resolved?
                      Wait til 11:20 tomorrow - or til the Timer ticks. :-D
                      Armin
                      Thanks for all your posts...
                      I'm still struggling though...
                      All I'm trying to do is open a form with a few buttons on, (which
                      action different events, not a problem with that) but at the same
                      time start a timer which puts a message up if no button is
                      pressed, but if one of the buttons are pressed, the timer is
                      reset.
                      I was hoping that by using a timer on the form I would just be
                      able to call it start and stop it when an action was performed.
                      Am I even using the correct timer ? as I understand there are
                      three different timers, windows.forms.t imers.timer, system.timers
                      and a threading timer...
                      Having looked at what I'm doing I seem to using a mixture !!!(Not
                      by design) of the windows.form.ti mers.timer and the
                      system.timers.t imer !!!
                      Where do I actually put the code for this ? is it on the form, or
                      is it in it's own timer class ?
                      >
                      P.S. the link to the msdn page I got my original code from is :-
                      http://msdn2.microsoft.com/en-us/lib...er(vs.71).aspx
                      First, which timer to use:
                      http://msdn2.microsoft.com/en-us/lib...e6(VS.80).aspx
                      http://msdn.microsoft.com/msdnmag/is...T/default.aspx


                      Second, did you read my first post? I'm gonna quote:

                      "- Did you set the sub main below as the startup object?
                      - The Timer in the Form has no purpose because the Form is never shown.
                      - Is the project type "console application"? Othwerwise you don't see the
                      output in the console (because you don't see the console)."


                      So, you must decide whether you want a Console application or a
                      WindowsApplicat ion. Set it in the project properties under "applicatio n
                      type". Maybe it's possible to mix it, but that's currently not the point.

                      Now,,, in any application you have a Sub Main, the starting point of every
                      appliation. In a WindowsApplicat ion, you can decide whether you a) specify a
                      Form as the startup object (in the project properteis), or b) write Sub Main
                      on your own. If you go for a), VB generates the invisible Sub Main which
                      mainly contains showing the startup Form. If you go for b), you are free in
                      if and when to show which Form.

                      If you change the application type to "console application" and set the Sub
                      Main that you posted as the "startup object", the timer should work.

                      If you want to test the Windows.Forms.T imer, you must set the application
                      type to "Windows application" and set the Form as the startup object. In
                      addition, you must add an event handler to the Timer's Tick event - the
                      Timer on the Form (by double-clicking the Timer icon on the designer).
                      Be aware that, now, it doesn't make sense anymore to use Console.Writlin e
                      because there is no console window in a "Windows application". You can
                      always write to the debug output using Debug.Writeline .


                      Armin

                      Comment

                      • Hotrod2000

                        #12
                        Re: Getting a timer to work

                        On 23 Aug, 16:30, "Armin Zingler" <az.nos...@free net.dewrote:
                        "Hotrod2000 " <pbrun...@bmsel land.comschrieb
                        >
                        >
                        >
                        >
                        >
                        On Aug 23, 1:33 pm, Hotrod2000 <pbrun...@bmsel land.comwrote:
                        On Aug 22, 11:10 pm, "Armin Zingler" <az.nos...@free net.dewrote:
                        >
                        "Matt F" <mfielderREMOVE C...@nospam.nos pamschrieb
                        >
                        LOL ---
                        >
                        So back to the original question --- did the original poster
                        ever get his issue resolved?
                        >
                        Wait til 11:20 tomorrow - or til the Timer ticks. :-D
                        >
                        Armin
                        >
                        Thanks for all your posts...
                        I'm still struggling though...
                        All I'm trying to do is open a form with a few buttons on, (which
                        action different events, not a problem with that) but at the same
                        time start a timer which puts a message up if no button is
                        pressed, but if one of the buttons are pressed, the timer is
                        reset.
                        I was hoping that by using a timer on the form I would just be
                        able to call it start and stop it when an action was performed.
                        Am I even using the correct timer ? as I understand there are
                        three different timers, windows.forms.t imers.timer, system.timers
                        and a threading timer...
                        Having looked at what I'm doing I seem to using a mixture !!!(Not
                        by design) of the windows.form.ti mers.timer and the
                        system.timers.t imer !!!
                        Where do I actually put the code for this ? is it on the form, or
                        is it in it's own timer class ?
                        >
                        P.S. the link to the msdn page I got my original code from is :-
                        http://msdn2.microsoft.com/en-us/lib...er(vs.71).aspx
                        >
                        First, which timer to use:http://msdn2.microsoft.com/en-us/lib...T/default.aspx
                        >
                        Second, did you read my first post? I'm gonna quote:
                        >
                        "- Did you set the sub main below as the startup object?
                        - The Timer in the Form has no purpose because the Form is never shown.
                        - Is the project type "console application"? Othwerwise you don't see the
                        output in the console (because you don't see the console)."
                        >
                        So, you must decide whether you want a Console application or a
                        WindowsApplicat ion. Set it in the project properties under "applicatio n
                        type". Maybe it's possible to mix it, but that's currently not the point.
                        >
                        Now,,, in any application you have a Sub Main, the starting point of every
                        appliation. In a WindowsApplicat ion, you can decide whether you a) specify a
                        Form as the startup object (in the project properteis), or b) write Sub Main
                        on your own. If you go for a), VB generates the invisible Sub Main which
                        mainly contains showing the startup Form. If you go for b), you are free in
                        if and when to show which Form.
                        >
                        If you change the application type to "console application" and set the Sub
                        Main that you posted as the "startup object", the timer should work.
                        >
                        If you want to test the Windows.Forms.T imer, you must set the application
                        type to "Windows application" and set the Form as the startup object. In
                        addition, you must add an event handler to the Timer's Tick event - the
                        Timer on the Form (by double-clicking the Timer icon on the designer).
                        Be aware that, now, it doesn't make sense anymore to use Console.Writlin e
                        because there is no console window in a "Windows application". You can
                        always write to the debug output using Debug.Writeline .
                        >
                        Armin- Hide quoted text -
                        >
                        - Show quoted text -
                        Hi Armin,

                        Thanks for all your help. I'll be more careful in the future coping
                        code from web-sites as I didn't realise that the original code I
                        posted was for a console !! until you helped me to get it working !!!

                        I only ever intended to use my timer on a windows form.
                        Anyway with your help I've managed to get it working as I intended and
                        I've posted my code below which works fine for what I wanted ...

                        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                        System.EventArg s) Handles Button1.Click
                        Timer1.Enabled = False
                        Dim f1 As New Form2
                        f1.Show()
                        Button1.Enabled = False
                        Button2.Enabled = False
                        Button3.Enabled = False
                        AddHandler f1.Closed, New EventHandler(Ad dressOf
                        OnForm2Closed)
                        f1.Show()
                        End Sub

                        Private Sub Button2_Click(B yVal sender As System.Object, ByVal e
                        As System.EventArg s) Handles Button2.Click
                        Timer1.Enabled = False
                        Dim f1 As New Form3
                        f1.Show()
                        Button1.Enabled = False
                        Button2.Enabled = False
                        Button3.Enabled = False
                        AddHandler f1.Closed, New EventHandler(Ad dressOf
                        OnForm3Closed)
                        f1.Show()
                        End Sub

                        Private Sub Button3_Click(B yVal sender As System.Object, ByVal e
                        As System.EventArg s) Handles Button3.Click
                        Application.Exi t()
                        End Sub
                        Private Sub OnForm2Closed(B yVal sender As Object, ByVal e As
                        EventArgs)
                        Button1.Enabled = True
                        Button2.Enabled = True
                        Button3.Enabled = True
                        Timer1.Enabled = True

                        End Sub
                        Private Sub OnForm3Closed(B yVal sender As Object, ByVal e As
                        EventArgs)
                        Button1.Enabled = True
                        Button2.Enabled = True
                        Button3.Enabled = True
                        Timer1.Enabled = True

                        End Sub


                        Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
                        System.EventArg s) Handles Timer1.Tick
                        Form1.ActiveFor m.Opacity = 0.5
                        Timer1.Stop()
                        MessageBox.Show ("press a button then...")
                        Form1.ActiveFor m.Opacity = 100
                        Timer1.Start()

                        End Sub

                        Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
                        System.EventArg s) Handles MyBase.Load
                        Timer1.Start()
                        Timer1.Interval = 2000

                        End Sub

                        Thanks again...
                        Paul

                        Comment

                        Working...