Link to home
Start Free TrialLog in
Avatar of ddepuemd
ddepuemd

asked on

simple timer to display current time with vs 2008

I am very confused about the way threading and implementing timers is being used in 2.0 and above.  I have read most everything I can on MS and elsewhere to implement a thread safe call to simply write the current time to a label on a form and just can't seem to get it through my thick skull.  Can someone PLEASE explain it for me??

Here is what I am doing:

Private WithEvents mytimer As System.Timers.Timer
Delegate Sub CurrentTimerCallBack(ByVal [text] As String)
Private WithEvents CurrentTimeWorker As BackgroundWorker

Private Sub SetText(ByVal [text] As String)
        If Me.lblCurrentTime.InvokeRequired Then
            Dim d As New CurrentTimerCallBack(AddressOf SetText)
            Me.Invoke(d, New Object() {[text]})
        Else
            Me.lblCurrentTime.Text = [text]
        End If
End Sub

Public Sub StartCurrentTimeTimer()
        If IsNothing(mytimer) Then
            mytimer = New System.Timers.Timer(1000)
            AddHandler mytimer.Elapsed, AddressOf OnCurrentTimeTimer
            mytimer.Interval = 1000
            mytimer.Enabled = True
        End If
    End Sub

    Private Sub StopCurrentTimeTimer()
        mytimer.Enabled = False
        mytimer = Nothing
    End Sub
    Private Sub OnCurrentTimeTimer(ByVal sender As Object, ByVal e As ElapsedEventArgs)
        Dim newhour As Integer
        newhour = Now.Hour
        Dim ampm, newsecond, newmin As String
        If Now.Second < 10 Then
            newsecond = "0" & Now.Second
        Else
            newsecond = Now.Second
        End If
        If Now.Minute < 10 Then
            newmin = "0" & Now.Minute
        Else
            newmin = Now.Minute
        End If
        If newhour > 12 Then
            ampm = "PM"
            newhour = newhour - 12
        Else
            ampm = "AM"
        End If
        lblCurrentTime.Text = newhour & ":" & newmin & "." & newsecond & " " & ampm
    End Sub

Now how in the heck do I start this thing, write the current time to the form, and stop it if I need to later?????

HELP!!
Avatar of ddepuemd
ddepuemd

ASKER

I got it to work now by simply changing to:

SetText(newhour & ":" & newmin & "." & newsecond & " " & ampm) in the oncurrent sub.

Will this still be thread safe?
Not sure why you need this to be on a BGW, it should work fine without it. If you have some things running that are weighing down the main thread on your app and your worried it be make this time inaccurate - you should be sending those things to the BGW. And then you won't have to worry about starting and stoping the BGW and cross-thread calls.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol, hey Idle_Mind nothing to do on a sunday :)
Someone is feeling generous : )
Hehe...exactly...  ;)
Thank you very much dude!  I will keep all of these in mind as I move on in this project...