none
timespan has negativ Value with stopwatch RRS feed

  • Question

  • Hello, i have tested the following samplecode from MSDN on my PC, but after 3 Seconds
    the timespan is getting a negative value.(stopwatch.elapsed.milliseconds)
    On every other machine the sample code works fine.

     

    Any Suggestions?

     

    Imports System

    Imports System.Diagnostics

    Imports System.Windows.Forms

    Imports System.Drawing

    Imports System.Drawing.Drawing2D

    Public Class formMain

    Inherits System.Windows.Forms.Form

    Dim stopWatch As StopWatch

    Dim captureLap As Boolean

    Public Sub New()

    MyBase.New()

    stopwatch = New StopWatch()

    captureLap = False

    InitializeComponent()

    End Sub

    ' Override dispose method to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

    If disposing Then

    If Not (components Is Nothing) Then

    components.Dispose()

    End If

    End If

    MyBase.Dispose(disposing)

    End Sub

    Shared Sub Main()

    Application.Run(New formMain)

    End Sub

    ' Define event handlers for various form events.

    Private Sub buttonStartStop_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles buttonStartStop.Click

    ' When the timer is stopped, this button event starts

    ' the timer. When the timer is running, this button

    ' event stops the timer.

    If stopWatch.IsRunning Then

    ' Stop the timer; show the start and reset buttons.

    stopWatch.Stop()

    buttonStartStop.Text = "Start"

    buttonLapReset.Text = "Reset"

    Else

    ' Start the timer; show the stop and lap buttons.

    stopWatch.Start()

    buttonStartStop.Text = "Stop"

    buttonLapReset.Text = "Lap"

    labelLap.Visible = False

    labelLapPrompt.Visible = False

    End If

    End Sub

    Private Sub buttonLapReset_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles buttonLapReset.Click

    ' When the timer is stopped, this button event resets

    ' the timer. When the timer is running, this button

    ' event captures a lap time.

    If buttonLapReset.Text = "Lap" Then

    If stopWatch.IsRunning Then

    ' Set the object state so that the next

    ' timer tick will display the lap time value.

    captureLap = True

    labelLap.Visible = True

    labelLapPrompt.Visible = True

    End If

    Else

    ' Reset the stopwatch and the displayed timer value.

    labelTime.Text = "00:00:00.00"

    buttonLapReset.Text = "Lap"

    stopWatch.Reset()

    labelLap.Visible = False

    labelLapPrompt.Visible = False

    End If

    End Sub

    Private Sub timerMain_Tick(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles timerMain.Tick

    ' When the timer is running, update the displayed timer

    ' value for each tick event.

    If stopWatch.IsRunning Then

    ' Get the elapsed time as a TimeSpan value.

    Dim ts As TimeSpan = stopWatch.Elapsed

    ' Format and display the TimeSpan value.

    labelTime.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", _

    ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)

    ' If the user has just clicked the "Lap" button,

    ' then capture the current time for the lap time.

    If captureLap Then

    labelLap.Text = labelTime.Text

    captureLap = False

    End If

    End If

    End Sub

     

    Private Sub labelExit_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles labelExit.Click

    ' Close the form when the user clicks the close button.

    Me.Close()

    End Sub

    Private Sub labelTopLeft_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles labelTopLeft.Click

    Me.Location = New Point(0, 0)

    End Sub

    Private Sub labelBottomLeft_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles labelBottomLeft.Click

    Me.Location = New Point(0, Screen.PrimaryScreen.WorkingArea.Height - Me.Height)

    End Sub

    Private Sub labelTopRight_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles labelTopRight.Click

    Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width - Me.Width, 0)

    End Sub

    Private Sub labelBottomRight_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles labelBottomRight.Click

    Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width - Me.Width, Screen.PrimaryScreen.WorkingArea.Height - Me.Height)

    End Sub

    Private Sub formMain_Click(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles MyBase.Click

    Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2)

    End Sub

    Private Sub formMain_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load

    DrawForm()

    Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2)

    End Sub

    Private Sub DrawForm()

    ' Shape the viewer form with rounded edges.

    Dim GraphicsPath As New Drawing2D.GraphicsPath

    GraphicsPath.AddArc(0, 0, 100, 100, 180, 90)

    GraphicsPath.AddArc(100, 0, 100, 100, 270, 90)

    GraphicsPath.AddArc(100, 100, 100, 100, 0, 90)

    GraphicsPath.AddArc(0, 100, 100, 100, 90, 90)

    Me.Region = New Region(GraphicsPath)

    End Sub

    Private components As System.ComponentModel.IContainer

    Private WithEvents labelExit As System.Windows.Forms.Label

    Private WithEvents labelTime As System.Windows.Forms.Label

    Private WithEvents timerMain As System.Windows.Forms.Timer

    Private WithEvents buttonLapReset As System.Windows.Forms.Button

    Private WithEvents buttonStartStop As System.Windows.Forms.Button

    Private WithEvents labelLapPrompt As System.Windows.Forms.Label

    Private WithEvents labelTimePrompt As System.Windows.Forms.Label

    Private WithEvents labelLap As System.Windows.Forms.Label

    Private WithEvents labelBottomLeft As System.Windows.Forms.Label

    Private WithEvents labelBottomRight As System.Windows.Forms.Label

    Private WithEvents labelTopLeft As System.Windows.Forms.Label

    Private WithEvents labelTopRight As System.Windows.Forms.Label

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    Me.components = New System.ComponentModel.Container

    Me.labelExit = New System.Windows.Forms.Label

    Me.labelTime = New System.Windows.Forms.Label

    Me.buttonLapReset = New System.Windows.Forms.Button

    Me.timerMain = New System.Windows.Forms.Timer(Me.components)

    Me.labelLap = New System.Windows.Forms.Label

    Me.buttonStartStop = New System.Windows.Forms.Button

    Me.labelLapPrompt = New System.Windows.Forms.Label

    Me.labelTimePrompt = New System.Windows.Forms.Label

    Me.labelBottomLeft = New System.Windows.Forms.Label

    Me.labelBottomRight = New System.Windows.Forms.Label

    Me.labelTopLeft = New System.Windows.Forms.Label

    Me.labelTopRight = New System.Windows.Forms.Label

    Me.SuspendLayout()

    '

    'labelExit

    '

    Me.labelExit.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

    Me.labelExit.ForeColor = System.Drawing.Color.Aqua

    Me.labelExit.Location = New System.Drawing.Point(160, 16)

    Me.labelExit.Name = "labelExit"

    Me.labelExit.Size = New System.Drawing.Size(20, 20)

    Me.labelExit.TabIndex = 0

    Me.labelExit.Text = "x"

    Me.labelExit.TextAlign = System.Drawing.ContentAlignment.BottomCenter

    '

    'labelTime

    '

    Me.labelTime.Font = New System.Drawing.Font("Comic Sans MS", 18.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.labelTime.ForeColor = System.Drawing.Color.Yellow

    Me.labelTime.Location = New System.Drawing.Point(-3, 56)

    Me.labelTime.Name = "labelTime"

    Me.labelTime.Size = New System.Drawing.Size(208, 32)

    Me.labelTime.TabIndex = 1

    Me.labelTime.Text = "00:00:00.00"

    '

    'buttonLapReset

    '

    Me.buttonLapReset.Font = New System.Drawing.Font("Comic Sans MS", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.buttonLapReset.ForeColor = System.Drawing.Color.Yellow

    Me.buttonLapReset.Location = New System.Drawing.Point(104, 160)

    Me.buttonLapReset.Name = "buttonLapReset"

    Me.buttonLapReset.Size = New System.Drawing.Size(72, 32)

    Me.buttonLapReset.TabIndex = 4

    Me.buttonLapReset.Text = "Lap"

    '

    'timerMain

    '

    Me.timerMain.Enabled = True

    Me.timerMain.Interval = 50

    '

    'labelLap

    '

    Me.labelLap.Font = New System.Drawing.Font("Comic Sans MS", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.labelLap.ForeColor = System.Drawing.Color.Yellow

    Me.labelLap.Location = New System.Drawing.Point(0, 120)

    Me.labelLap.Name = "labelLap"

    Me.labelLap.Size = New System.Drawing.Size(200, 32)

    Me.labelLap.TabIndex = 5

    Me.labelLap.Visible = False

    '

    'buttonStartStop

    '

    Me.buttonStartStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat

    Me.buttonStartStop.Font = New System.Drawing.Font("Comic Sans MS", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.buttonStartStop.ForeColor = System.Drawing.Color.Yellow

    Me.buttonStartStop.Location = New System.Drawing.Point(24, 160)

    Me.buttonStartStop.Name = "buttonStartStop"

    Me.buttonStartStop.Size = New System.Drawing.Size(72, 32)

    Me.buttonStartStop.TabIndex = 2

    Me.buttonStartStop.Text = "Start"

    '

    'labelLapPrompt

    '

    Me.labelLapPrompt.Font = New System.Drawing.Font("Comic Sans MS", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.labelLapPrompt.ForeColor = System.Drawing.Color.Yellow

    Me.labelLapPrompt.Location = New System.Drawing.Point(8, 96)

    Me.labelLapPrompt.Name = "labelLapPrompt"

    Me.labelLapPrompt.Size = New System.Drawing.Size(96, 24)

    Me.labelLapPrompt.TabIndex = 6

    Me.labelLapPrompt.Text = "Lap Time"

    Me.labelLapPrompt.Visible = False

    '

    'labelTimePrompt

    '

    Me.labelTimePrompt.Font = New System.Drawing.Font("Comic Sans MS", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.labelTimePrompt.ForeColor = System.Drawing.Color.Yellow

    Me.labelTimePrompt.Location = New System.Drawing.Point(0, 24)

    Me.labelTimePrompt.Name = "labelTimePrompt"

    Me.labelTimePrompt.Size = New System.Drawing.Size(88, 24)

    Me.labelTimePrompt.TabIndex = 7

    Me.labelTimePrompt.Text = "Time"

    '

    'labelBottomLeft

    '

    Me.labelBottomLeft.BackColor = System.Drawing.Color.Black

    Me.labelBottomLeft.ForeColor = System.Drawing.Color.Black

    Me.labelBottomLeft.Location = New System.Drawing.Point(0, 176)

    Me.labelBottomLeft.Name = "labelBottomLeft"

    Me.labelBottomLeft.Size = New System.Drawing.Size(16, 16)

    Me.labelBottomLeft.TabIndex = 8

    '

    'labelBottomRight

    '

    Me.labelBottomRight.BackColor = System.Drawing.Color.Black

    Me.labelBottomRight.ForeColor = System.Drawing.Color.Black

    Me.labelBottomRight.Location = New System.Drawing.Point(184, 176)

    Me.labelBottomRight.Name = "labelBottomRight"

    Me.labelBottomRight.Size = New System.Drawing.Size(16, 16)

    Me.labelBottomRight.TabIndex = 9

    '

    'labelTopLeft

    '

    Me.labelTopLeft.BackColor = System.Drawing.Color.Black

    Me.labelTopLeft.ForeColor = System.Drawing.Color.Black

    Me.labelTopLeft.Location = New System.Drawing.Point(0, 8)

    Me.labelTopLeft.Name = "labelTopLeft"

    Me.labelTopLeft.Size = New System.Drawing.Size(16, 16)

    Me.labelTopLeft.TabIndex = 10

    '

    'labelTopRight

    '

    Me.labelTopRight.BackColor = System.Drawing.Color.Black

    Me.labelTopRight.ForeColor = System.Drawing.Color.Black

    Me.labelTopRight.Location = New System.Drawing.Point(184, 8)

    Me.labelTopRight.Name = "labelTopRight"

    Me.labelTopRight.Size = New System.Drawing.Size(16, 16)

    Me.labelTopRight.TabIndex = 11

    '

    'formMain

    '

    Me.AutoScaleBaseSize = New System.Drawing.Size(9, 22)

    Me.BackColor = System.Drawing.Color.Navy

    Me.ClientSize = New System.Drawing.Size(200, 200)

    Me.Controls.Add(Me.labelTopRight)

    Me.Controls.Add(Me.labelTopLeft)

    Me.Controls.Add(Me.labelBottomRight)

    Me.Controls.Add(Me.labelBottomLeft)

    Me.Controls.Add(Me.labelTimePrompt)

    Me.Controls.Add(Me.labelLapPrompt)

    Me.Controls.Add(Me.labelLap)

    Me.Controls.Add(Me.buttonLapReset)

    Me.Controls.Add(Me.buttonStartStop)

    Me.Controls.Add(Me.labelTime)

    Me.Controls.Add(Me.labelExit)

    Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None

    Me.Name = "formMain"

    Me.Text = "StopWatch Sample"

    Me.TopMost = True

    Me.ResumeLayout(False)

    End Sub

    End Class

     

    Monday, March 10, 2008 3:04 PM

Answers

  • Stopwatch does call QueryPerformanceCounter which may or may not use a CPU cycle counter or a motherboard timer (if supported). I suspect you have a multicore CPU where your thread might be scheduled to another CPU. The counters of both CPUs are not equal since this counter does count the actually executed cpu cycles which might be more or less depending on load and the power state (CPU throttling is now even on Desktop PCs very present).
    You could try to set the CPU affinity of yoru process to one specifc CPU to prevent scheduling to another CPU but the power save modes will still affect your which dynamically changes the clock rate of the CPU.

    The problem with your CPU has already been reported here:
    http://66.102.9.104/search?q=cache:gtw5oHBUVGsJ:channel9.msdn.com/ShowPost.aspx%3FPostID%3D156175+queryperformancecounter+cpu+throttling&hl=de&ct=clnk&cd=2&gl=de
    It could help to disable certain power save functions via registry keys like it is shown in some articles.

    Yours,
      Alois Kraus

    Tuesday, March 11, 2008 11:20 PM

All replies