【问题标题】:Countdown not working (Using Stopwatch Class + Dispatchertimer)倒计时不起作用(使用秒表类 + 调度器定时器)
【发布时间】:2023-04-02 10:31:01
【问题描述】:

我正在初始化小时、分钟和秒

然后将其转换为总毫秒数。

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

elms = cms - e.Milliseconds

(计算的毫秒数 = 计算的毫秒数。- Stopwatch.elapsedMilliseconds)

然后将评估的毫秒转换回 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

【问题讨论】:

  • @DanielHilgarth 我无法触发倒数计时器,下面指定的方法也不起作用

标签: .net vb.net stopwatch countdowntimer dispatchertimer


【解决方案1】:

错误在这里:

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

好吗?

【讨论】:

  • No Not Okay,当我执行它并随时启动并调用 Start() 它只是变成 00:00:00.00
【解决方案2】:

你把事情复杂化了。您需要做的就是将总时间存储为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

【讨论】:

  • 非常感谢!实际上我是第一次创建倒计时:) 所以只是搞砸了逻辑。感谢您消除我的疑虑
  • 我在这里遇到运行时错误:'Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text)) ' 输入字符串的格式不正确。
  • 您将添加代码来处理无效输入。您可以通过在该行周围放置一个 try/catch 块或使用 Integer.TryParse 而不是 Integer.Parse 来做到这一点。
  • Ahan 不工作错误:重载解析失败,因为没有可访问的“TryParse”接受此数量的参数。
  • 仍然无法正常工作:/ Dim total As New TimeSpan(Integer.Parse(Hours.Text), Integer.Parse(Minutes.Text), Integer.Parse(Seconds.Text)) 错误:输入字符串的格式不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多