Hi, I'm trying to create my own Timer that allows me to show elapsed time
I create the follow class but it returns thread problem where I assign the countdown value in a label (or any component) of the form,
How can i solve?
How to test:
Create a form as Form1
add the TimerR as TimerR1
set TimerR1.enable to true
set TimerR1.ActiveCountDown to False
At the event TimerR1_CountDown set the Form1 Text to e.CountDownSeconds
The Class
Public Class TimerR
Inherits System.Windows.Forms.Timer
Dim WithEvents aTimer As New System.Timers.Timer
Public Class EventArgs_CountDown
Inherits EventArgs
Public TimerIsActive As Boolean
Public TimerInterval As Integer
Public ElapsedSeconds As Integer
Public ElapsedMilliSeconds As Integer
Public CountDownSeconds As Integer
Public ErrorMessage As String
End Class
Public Shared Event CountDown(sender As Object, e As EventArgs_CountDown)
Public Sub New()
Me.Interval = 10000
End Sub
Private _ActiveCountDown As Boolean = False
Public Property ActiveCountDown As Boolean
Get
Return _ActiveCountDown
End Get
Set(ByVal Value As Boolean)
_ActiveCountDown = Value
aTimer.AutoReset = True
aTimer.Interval = 1000 '1 second
If _ActiveCountDown Then
AddHandler aTimer.Elapsed, AddressOf InternalTick
aTimer.Start()
Else
RemoveHandler aTimer.Elapsed, AddressOf InternalTick
aTimer.Stop()
End If
End Set
End Property
Friend Sub InternalTick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
Static Elapsed As Long = 0
Dim e1 As New EventArgs_CountDown
e1.TimerIsActive = Me.Enabled
Try
If e1.TimerIsActive = False Then
Elapsed = 0
e1.ElapsedSeconds = 0
e1.ElapsedMilliSeconds = 0
e1.CountDownSeconds = 0
Else
Elapsed += aTimer.Interval
e1.ElapsedSeconds = Elapsed / 1000
e1.ElapsedMilliSeconds = Elapsed
e1.CountDownSeconds = Int((Me.Interval / 1000) - e1.ElapsedSeconds)
End If
e1.TimerInterval = Me.Interval
e1.ErrorMessage = ""
Catch ex As Exception
e1.ErrorMessage = ex.Message
End Try
RaiseEvent CountDown(sender, e1)
End Sub
End Class
I create the follow class but it returns thread problem where I assign the countdown value in a label (or any component) of the form,
How can i solve?
How to test:
Create a form as Form1
add the TimerR as TimerR1
set TimerR1.enable to true
set TimerR1.ActiveCountDown to False
At the event TimerR1_CountDown set the Form1 Text to e.CountDownSeconds
The Class
Public Class TimerR
Inherits System.Windows.Forms.Timer
Dim WithEvents aTimer As New System.Timers.Timer
Public Class EventArgs_CountDown
Inherits EventArgs
Public TimerIsActive As Boolean
Public TimerInterval As Integer
Public ElapsedSeconds As Integer
Public ElapsedMilliSeconds As Integer
Public CountDownSeconds As Integer
Public ErrorMessage As String
End Class
Public Shared Event CountDown(sender As Object, e As EventArgs_CountDown)
Public Sub New()
Me.Interval = 10000
End Sub
Private _ActiveCountDown As Boolean = False
Public Property ActiveCountDown As Boolean
Get
Return _ActiveCountDown
End Get
Set(ByVal Value As Boolean)
_ActiveCountDown = Value
aTimer.AutoReset = True
aTimer.Interval = 1000 '1 second
If _ActiveCountDown Then
AddHandler aTimer.Elapsed, AddressOf InternalTick
aTimer.Start()
Else
RemoveHandler aTimer.Elapsed, AddressOf InternalTick
aTimer.Stop()
End If
End Set
End Property
Friend Sub InternalTick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
Static Elapsed As Long = 0
Dim e1 As New EventArgs_CountDown
e1.TimerIsActive = Me.Enabled
Try
If e1.TimerIsActive = False Then
Elapsed = 0
e1.ElapsedSeconds = 0
e1.ElapsedMilliSeconds = 0
e1.CountDownSeconds = 0
Else
Elapsed += aTimer.Interval
e1.ElapsedSeconds = Elapsed / 1000
e1.ElapsedMilliSeconds = Elapsed
e1.CountDownSeconds = Int((Me.Interval / 1000) - e1.ElapsedSeconds)
End If
e1.TimerInterval = Me.Interval
e1.ErrorMessage = ""
Catch ex As Exception
e1.ErrorMessage = ex.Message
End Try
RaiseEvent CountDown(sender, e1)
End Sub
End Class