none
New System.Timer RRS feed

  • Question

  • I am finding that my code is failing when I am trying to initiate a new timer. Anyone know why?

    Public Class Form1
    
        Public WithEvents elapsedTimeTimer As System.Timers.Timer
    
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
                        elapsedTimeTimer = New System.Timers.Timer()
                        elapsedTimeTimer.Interval = 100
                        AddHandler elapsedTimeTimer.Elapsed, AddressOf Time
                        elapsedTimeTimer.Enabled = True
                        elapsedTimeTimer.Start()
    
            
    
        End Sub
    
    
    
    
    
    End Class

    This is crashing the app.

    Sunday, March 31, 2019 10:34 PM

All replies

  • Hi,

    I wrote two Time methods, you can compare the difference between the two.

    Public Class Form1
        Public WithEvents elapsedTimeTimer As System.Timers.Timer
        Dim i As Integer = 0
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            elapsedTimeTimer = New System.Timers.Timer()
            elapsedTimeTimer.Interval = 1000
            AddHandler elapsedTimeTimer.Elapsed, AddressOf Time
            elapsedTimeTimer.Enabled = True
            elapsedTimeTimer.Start()
    
        End Sub
        'error
        Public Sub Time()
            i += 1
            TextBox1.Text = i
        End Sub
    
        'no error
        Public Sub Time()
    
            TextBox1.Invoke(New Action(Sub()
                                           i += 1
                                           TextBox1.Text = i
                                       End Sub))
    
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 1, 2019 1:35 AM
  • Alex,

       I am using an Invoke method:

       
        Delegate Function DelegateTimer2(ByVal CH As Integer) As Integer
          Public Function Time() As Integer
    
          Dim CallDelegateTimer2 As DelegateTimer2 = AddressOf whatever
    
            CallDelegateTimer2.Invoke(0)
    
    
        End function

    For some reason,  if  I take out my "addHandler" to the timer code app hangs.   Even if I simply add the timer ( not even enabling it)  I get a crash.  The invoke I think is OK. 




    • Edited by -tE Monday, April 1, 2019 4:56 PM
    Monday, April 1, 2019 2:33 AM
  • Hi,

    I don't know if your whatever method is getting an error? Please provide the full code?

    Bets Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by -tE Monday, April 1, 2019 12:02 PM
    • Unmarked as answer by -tE Monday, April 1, 2019 4:39 PM
    Monday, April 1, 2019 5:21 AM
  • If you only have one timer then do it this way, and FWIW if you have multiple then the code should fail.

    Public Class Form1
        Public WithEvents elapsedTimeTimer As New System.Timers.Timer
    
        Private Sub elapsedTimeTimer_Elapsed(sender As Object,
                                             e As ElapsedEventArgs) Handles elapsedTimeTimer.Elapsed
            Debug.WriteLine(e.SignalTime)
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            elapsedTimeTimer.Interval = 100
            elapsedTimeTimer.Enabled = True
            elapsedTimeTimer.Start()
        End Sub
    End Class
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Monday, April 1, 2019 1:23 PM
  • Hi,

    I don't know if your whatever method is getting an error? Please provide the full code?

    Bets Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    This what I have.

        Delegate Function DelegateTimer() As Integer
        Delegate Function DelegateTimer2(ByVal CH As Integer) As Integer
        Delegate Function ShowTime() As Integer
        Dim da As New ShowTime(AddressOf Time)
        Dim CallDelegateTimer2 As DelegateTimer2 = AddressOf getBufferLocation

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click elapsedTimeTimer = New System.Timers.Timer() elapsedTimeTimer.Interval = 100 AddHandler elapsedTimeTimer.Elapsed, AddressOf Time elapsedTimeTimer.Enabled = True elapsedTimeTimer.Start() End Sub Public Function Time() As Integer CallDelegateTimer2.Invoke(0)

    CallDelegateTimer2.Invoke('up to 64')

    End Function Public Function getBufferLocation(ByVal CH As Integer) As Integer

    If ListBox2.InvokeRequired Then Invoke(da) Else ListBox2.Items.Add(My data) ' The data is audio bytes. I left out all that code for not necessary to post all that. yet.' End If

    End Function


    As I have shown,  I took out all the code in the handler function so it shouldn't need Invoke anyway.  It freezes up for sure when the handler is there. Invoke is away to pass variables to a second function.  I am successfully doing that elsewhere.  I also use the invoke for displaying data in form components.  As well working fine.  Can't find this here though



    You are passing "New Action Sub"  through the invoke.  I am passing a integer.  But even if that line of code is out it hangs.
    • Edited by -tE Monday, April 1, 2019 5:04 PM
    Monday, April 1, 2019 4:46 PM
  • Hi,

    Your code is missing

     Public WithEvents elapsedTimeTimer As System.Timers.Timer

    Public Class Form1
        Delegate Function DelegateTimer() As Integer
        Delegate Function DelegateTimer2(ByVal CH As Integer) As Integer
        Delegate Function ShowTime() As Integer
        Dim da As New ShowTime(AddressOf Time)
        Dim CallDelegateTimer2 As DelegateTimer2 = AddressOf getBufferLocation
        Public WithEvents elapsedTimeTimer As System.Timers.Timer
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            elapsedTimeTimer = New System.Timers.Timer()
            elapsedTimeTimer.Interval = 1000
            AddHandler elapsedTimeTimer.Elapsed, AddressOf Time
            elapsedTimeTimer.Enabled = True
            elapsedTimeTimer.Start()
        End Sub
        Public Function Time() As Integer
    
            CallDelegateTimer2.Invoke(0)
    
        End Function
    
    
    
        Public Function getBufferLocation(ByVal CH As Integer) As Integer
            If ListBox2.InvokeRequired Then
    
                Invoke(da)
    
            Else
    
                'ListBox2.Items.Add(CH)
                Invoke(da)
                '  The data is audio bytes.  I left out all that code for not necessary to post all that. yet.'
    
            End If
    
        End Function
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Tuesday, April 2, 2019 2:50 AM