user3881273
user3881273

Reputation: 11

Timer does not work

I try to run a timer from my winform application. For some reason the function that should run on the timer's tick (IsTimeOffsetValid) is not called nor stopped on break point, and basically nothing happens. I attached a code sample below.

I appreciate the help.

Module Module1
    Sub main()
        Dim OutputForm As New Form17
        Application.Run(OutputForm)
    End Sub
End Module
Public Class Form17

    Private TimerServerOffset As New System.Timers.Timer

    Private Sub Form17_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler TimerServerOffset.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf IsTimeOffsetValid)
        TimerServerOffset.Interval = 1
        TimerServerOffset.Start()
    End Sub

    Private Sub IsTimeOffsetValid()
        MsgBox("IsTimeOffsetValid")
    End Sub

End Class

Upvotes: 0

Views: 1035

Answers (1)

Emond
Emond

Reputation: 50672

Apart from errors in the code that you posted there are other issues with the design.

Read this question: System.Timers.Timer vs System.Threading.Timer

The callback is called on a worker thread (not the UI thread) so displaying a message box could be a big problem.

then switch to a more fitting timer. If all you want to do is validate the inputs every second, switch to the System.Windows.Forms.Timer. The tick handler runs on the UI thread so you can change the UI in the handler.

Then consider changing the interval a message box popping up every millisecond is not possible and not user friendly.

Finally, I would suggest NOT using a timer for this: just handle changes to the input fields and respond to changed inputs or use the standard validation events of the WinForms controls. This is much cheaper (on the CPU) and will not mess with the focus.

Upvotes: 3

Related Questions