none
How to transfer the parameter in the Timer event? RRS feed

  • Question

  • I want to inoke the function A in the Timer1 event, how to do?

    Public Class MyClass
     
        Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()

        Private Shared Sub TimerEventProcessor(myObject As Object, ByVal myEventArgs As EventArgs) Handles myTimer.Tick
            myTimer.Stop()
           'How to invoke the function A?

        End Sub

        Private Shared Function A(Arg1 as String, Arg2 As String) As Int32
          'Do someting and return the integer
        End Function

        Public Shared Function SearchText(Arg1 as String, Arg2 As String) As Int32
            myTimer.Interval = 500
            myTimer.Stop()
            myTimer.Start()
           'How to return the function A value?
        End Function

    End Class

    Wednesday, April 12, 2017 6:44 AM

All replies

  • I want to inoke the function A in the Timer1 event, how to do?

       Private Shared Sub TimerEventProcessor(myObject As Object, ByVal myEventArgs As EventArgs) Handles myTimer.Tick
             myTimer.Stop()
             Dim Answer As Integer = A(String_1, String_2)
        End Sub

    Public Shared Function A(Arg1 as String, Arg2 As String) As Int32 Return (2017) End Function

    But I somehow suspect that isn't actually the problem.  What exactly prevents you from doing it like that?

    I can't see why you would stop the timer and immediately restart it - that stop/start seems redundant.


    • Edited by Acamar Wednesday, April 12, 2017 7:41 AM fmt
    Wednesday, April 12, 2017 7:37 AM
  • With that code: Never, there are so many errors in that code that you better can first handle those.

    Success
    Cor

    Wednesday, April 12, 2017 7:52 AM
  • I am making the Text Search function in the Text Changed event, when type the search text in the text box, return the matched lines in the Listview, but the return result will not be accurate if not use the timer control. it look like the code below, the Timer1 interval is 500ms.

      Private Sub txtFind_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFind.TextChanged
            Timer1.Stop()
            Timer1.Start()

        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            lblFound.Text = GetMatchedListViewItemCount(ListVieName, txtFind.Text, {1, 2, 3, 4})
        End Sub

    Now i need to add the function to the Class.

     
    • Edited by gaxjyxq Wednesday, April 12, 2017 8:10 AM
    Wednesday, April 12, 2017 8:10 AM
  • I am making the Text Search function in the Text Changed event, when type the search text in the text box, return the matched lines in the Listview, but the return result will not be accurate if not use the timer control. it look like the code below, the Timer1 interval is 500ms.

    That code does not make sense and does not appear to be related to the question you asked. A timer is not used to measure time.  It is used to trigger an event at periodic intervals.   What is the time that you are trying to measure?

    Wednesday, April 12, 2017 9:32 AM
  • I am making the Text Search function in the Text Changed event, when type the search text in the text box, return the matched lines in the Listview, but the return result will not be accurate if not use the timer control. it look like the code below, the Timer1 interval is 500ms.

      Private Sub txtFind_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFind.TextChanged
            Timer1.Stop()
            Timer1.Start()

        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            lblFound.Text = GetMatchedListViewItemCount(ListVieName, txtFind.Text, {1, 2, 3, 4})
        End Sub

    Now i need to add the function to the Class.

     

    Hi

    From what little information I can decipher, you may be better to use a DataTable bound to a DataGridView. That would allow easy filtering/searching/display of data.

    And, you don't need any Timers.


    Regards Les, Livingston, Scotland

    Wednesday, April 12, 2017 1:40 PM
  • Hi gaxjyxq,

    According to your description, you can create your own custom Timer with custom EventArgs and override the Timer's Tick event.

    Another easier solution might be to use the Tag property of the Timer. This property can hold any object. So you can set this property as the respective label.

    More detailed info, please refer to :https://social.msdn.microsoft.com/Forums/en-US/619eebe8-4dd5-455f-86cb-1dc552d2f3af/pass-parameter-to-timer-tick-event?forum=vblanguage

    Best Regards,

    Cherry Bu


    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.

    Thursday, April 13, 2017 8:03 AM
    Moderator