倒计时不起作用(使用秒表课程+ Dispatchertimer)

时间:2012-09-20 17:26:42

标签: .net vb.net stopwatch countdowntimer dispatchertimer

我正在启动小时,分钟和秒

然后将其转换为总毫秒。

然后我正在做的是从Elapsed MilliSeconds中减去Total MilliSeconds

elms = cms - e.Milliseconds

(评估毫秒=计算Millisec。 - Stopwatch.elapsedMilliseconds)

然后将Evaluated Milliseconds转换回HH:MM:SS:MS格式。

但由于某些逻辑错误而无法正常工作,这就是为什么我需要一些帮助。请帮我一点 这是我的代码:

        Dim dd As Integer = 0
        Dim mm As Integer = 0
        Dim hh As Integer = 0
        Dim ss As Integer = 0
        Dim ms As Integer = 0
        Dim cms As Long = 0
        Dim elms As Long = 0

    Dim stopwatch As System.Diagnostics.Stopwatch = New System.Diagnostics.Stopwatch
    Dim dt As DispatcherTimer = New DispatcherTimer

    Private Sub StartButton_Click(sender As Object, e As RoutedEventArgs) Handles StartButton.Click

                hh = Hours.Text * 60 * 60 * 1000
                mm = Minutes.Text * 60 * 1000
                ss = Seconds.Text * 1000
                cms = hh + mm + ss
                hh = mm = ss = 0
                Start()
    End Sub

    Private Sub Start()

                dt.Interval = New TimeSpan(0, 0, 0, 0, 10)
                AddHandler dt.Tick, AddressOf ontimertick
                stopwatch.Start()
                dt.Start()

            End If
    End Sub

    Private Sub ontimertick()


            Dim e As New TimeSpan
            e = stopwatch.Elapsed

                elms = cms - e.Milliseconds
                ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
                mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
                hh = elms \ (1000 * 60 * 60)
                elms = elms Mod 1000

                MicroSeconds.Text = elms.ToString("00")
                Seconds.Text = ss.ToString("00")
                Minutes.Text = mm.ToString("00")
                Hours.Text = hh.ToString("00")
    End Sub

2 个答案:

答案 0 :(得分:0)

错误在于:

ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
hh = elms \ (1000 * 60 * 60)
elms = elms Mod 1000

必须是:

ss = ((elms Mod (1000 * 60 * 60)) Mod (1000 * 60)) \ 1000
 elms=elms-(ss*1000)
mm = (elms Mod (1000 * 60 * 60)) \ (1000 * 60)
 elms=elms-(mm*1000)
hh = elms \ (1000 * 60 * 60)
 elms=elms-(hh*1000)
elms = elms Mod 1000

OK?

答案 1 :(得分:0)

你过度复杂了。您需要做的就是将总时间存储为TimeSpan对象。然后,您可以从中减去StopWatch.Elapsed时间,因为它也是TimeSpan。然后,计算得出的TimeSpan对象将包含剩余时间。例如:

' Get the total desired time for count down
Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))

' Later, get the time left and display on screen
Dim timeLeft As TimeSpan = total - stopwatch.Elapsed
Hours.Text = timeLeft.TotalHours
Minutes.Text = timeLeft.TotalMinutes
Seconds.Text = timeLeft.TotalSeconds

显然,只是这样调用Integer.Parse是不安全的,因为文本框可能包含非数字值。您需要添加代码来处理它。例如,如果您想要在输入无效条目时默认为零,则可以执行以下操作:

Dim totalHours As Integer
Dim totalMinutes As Integer
Dim TotalSeconds As Integer
Integer.TryParse(Hours.Text, totalHours)
Integer.TryParse(Minutes.Text, totalMinutes)
Integer.TryParse(Seconds.Text, totalSeconds)
Dim total As New TimeSpan(totalHours, totalMinutes, totalSeconds)

或者,如果要显示验证错误消息,可以执行以下操作:

Dim total As TimeSpan
Try
    total = New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text))
Catch ex As FormatException
    MessageBox.Show("Entries must be numeric.")
End Try