Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i stucked with a timer, can someone explain please how this event works? what to call in main form, to make this module start? I created a module called "Example", tried to make timer write in form1 label text "hello" every 2seconds, but when i start app nothing happens. Any help? Thanks. Im using WinForms

What I have tried:

Imports System.Timers
Public Module example
    Private aTimer As Timer
    Public Sub Main()

        ' Create a timer and set a two second interval.
        aTimer = New System.Timers.Timer()
        aTimer.Interval = 2000

        ' Hook up the Elapsed event for the timer.  
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        ' Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = True

        ' Start the timer
        aTimer.Enabled = True

    End Sub
    Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
        Form1.Label1.Text = " hello"
    End Sub

End Module
Posted
Updated 4-Jul-17 15:47pm
v2

It doesn't work because the Elapsed event is raised on a different thread[^] from the UI thread (startup). You cannot touch UI controls from anything other than the UI thread. In this case, you're going to have to Invoke a method on the UI thread to change the text in the TextBox.
VB.NET
Private aTimer As Timer = Nothing

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    aTimer = New Timer() With {
        .Interval = 2000
    }

    ' Hook up the Elapsed event for the timer.
    AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

    ' Have the timer fire repeated events (true is the default)
    aTimer.AutoReset = True

    ' Start the timer
    aTimer.Enabled = True
End Sub

Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
    SetTextBoxText("Elapsed")
End Sub

Private Sub SetTextBoxText(message As String)
    If Me.InvokeRequired Then
        Invoke(Sub()
                   SetTextBoxText(message)
               End Sub)
    Else
        TextBox1.Text = "Elapsed"
    End If
End Sub
 
Share this answer
 
v2
Comments
JeezyWonder 4-Jul-17 22:58pm    
THanks a lot, i wanted to figure out timer usage, because i want to make timer event always check for avaiblae serial ports, and if its any of them auto connect on them. Can you check this please:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
aTimer = New Timer() With {
.Interval = 2000
}

' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

' Have the timer fire repeated events (true is the default)
aTimer.AutoReset = True

' Start the timer
aTimer.Enabled = True
end sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
SetTextBoxText("Elapsed")
End Sub

Public Sub SetTextBoxText(message As String)
If Me.InvokeRequired Then
Invoke(Sub()
SetTextBoxText(message)
End Sub)
Else
If SerialPort1.IsOpen = False Then
Dim Portnames As String() = SerialPort.GetPortNames
cboComPort.Items.AddRange(Portnames)
cboComPort.Text = Portnames(0)
SerialPort1.PortName = cboComPort.Text
SerialPort1.Open()
End If
End if
End Sub
Oops...
VB
' Start the timer
aTimer.Start
 
Share this answer
 
v2
Comments
Dave Kreskowiak 4-Jul-17 21:50pm    
Not needed. Setting Enabled to True does the same thing.
[no name] 4-Jul-17 21:55pm    
Yes, you're right. Sorry about that. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900