Question Another Stopwatch Class (framework 3.5) question

sisquo76

Active member
Joined
Dec 1, 2008
Messages
28
Location
Bosnia and Herzegovina
Programming Experience
3-5
Is it possible to trigger event whenever seconds on stopwatch change (that means every second, but it has to be synchronized with seconds)?

Thanks,

sisquo76
 
No, StopWatch doesn't have events, use one of the Timers for this.
 
If you take a look at the interval property on the timer you'll see 'The frequency of Elapsed events in milliseconds'. If should have a default value of 100 already.
 
If you find the Forms.Timer lacking in precision use the Timers.Timer instead.
 
If you find the Forms.Timer lacking in precision use the Timers.Timer instead.

Ok, this is the problem. I have this code
VB.NET:
Public Function GameTime() As String
        Try
            Dim iMinutes As Integer = Int(Stopwatch.Elapsed.TotalMinutes)
            iMinutes += iAdditionalTime
            Dim sMinutes As String = iMinutes.ToString("##0")
            Dim sSeconds As String = Stopwatch.Elapsed.Seconds.ToString("00")
            Return sMinutes + ":" + sSeconds
        Catch
            Return "0:00"
        End Try
    End Function

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
   Dim SW As New StreamWriter(ReadFilesPath() + "\Stopwatch.txt", False, System.Text.Encoding.Default)
            SW.Write(GameTime)
            SW.Close()
    End Sub
Timer1 interval is set to 100ms. I'm saving this file to network share every 100ms, so I think this is too often. What I would like to do is to find the way to trigger event every time second changes in Stopwatch Class. Do you know any way to do this?

sisquo76
 
sisquo76 said:
saving this file to network share every 100ms, so I think this is too often.
Absoluty, keep the file open.
sisquo76 said:
What I would like to do is to find the way to trigger event every time second changes in Stopwatch Class / Is it possible to trigger event whenever seconds on stopwatch change
JohnH said:
No, StopWatch doesn't have events, use one of the Timers for this.
To write elapsed time each second just store start time (Date.Now) in a variable and trigger Timer 1000ms where you write the timespan (Date.Now-startTime). This is also how the StopWatch class works.
 
Back
Top