【问题标题】:Vb.Net something that i want to fix in my function - StopwatchVb.Net 我想在我的函数中修复的东西 - 秒表
【发布时间】:2012-06-14 13:14:34
【问题描述】:

我制作了将时间字符串(“hh\:mm\:ss\,fff” - 例如:“00:00:00,100”)转换为零件的函数
strTime = "00:00:00,100" =
h int = 0
m int = 0
sec int = 0
毫秒 int = 100


功能:

Public Function ShowInLabel(ByVal TEXT As String, ByVal time As String, ByVal startTime As Boolean) As Boolean
        On Error Resume Next
        Dim sss As String
        sss = time
        Dim start As String = StrReverse(sss)
        start = StrReverse(start.Substring(0, 3))
        Dim s As Integer
        s = Integer.Parse(start)
        Dim secstart As String = StrReverse(sss).Substring(0, 6)
        secstart = StrReverse(secstart)
        Dim secs As Integer = Integer.Parse(secstart.Substring(0, 2))
        Dim hurs As Integer = Integer.Parse(sss.Substring(0, 2))
        Dim mins As Integer = Integer.Parse(StrReverse(StrReverse(sss.Substring(0, 5)).Substring(0, 2)))

        Dim stopWatch As New Stopwatch()
        stopWatch.Start()
noh:
        If stopWatch.Elapsed.Hours = hurs Then
            GoTo yesh
        Else
            GoTo noh
        End If
yesh:
        If stopWatch.Elapsed.Minutes = mins Then
            GoTo yesm
        Else
            GoTo yesh
        End If
yesm:
        If stopWatch.Elapsed.Seconds = secs Then
            GoTo yess
        Else
            GoTo yesm
        End If
yess:

        If stopWatch.Elapsed.Milliseconds > s Or stopWatch.Elapsed.Milliseconds = s Then
            GoTo done
        Else
            GoTo yess
        End If
done:
        If startTime = False Then
            Label1.Text = ""
        Else
            Label1.Text = TEXT
        End If

        Return True

    End Function



例子:

ShowInLabel("SubTitle", "00:00:00,100", True)

函数有效,
但是当运行应用程序的函数被卡住 Till 函数返回 true
为什么会这样?

【问题讨论】:

  • 我看到GOTO 声明了吗?
  • 这个语句可以用什么代替?
  • 会不会是Windows Form开发?在这种情况下, Timer 类可能会有所帮助。它有 Tick 事件,既可以解决挂线问题,又可以帮助丢弃 goto。
  • @LarsTech,如果你仔细看你还会看到On Error Resume Next
  • 也许有人可以给我一个描述解决方案的例子?

标签: vb.net stopwatch


【解决方案1】:

你需要做的就是这样:

    Dim time As Date = DateTime.ParseExact("00:01:02,123", "hh:mm:ss,fff", CultureInfo.InvariantCulture)
    Dim h As Integer = time.Hour
    Dim m As Integer = time.Minute
    Dim sec As Integer = time.Second
    Dim millisec As Integer = time.Millisecond

但是,由于对您要完成的工作非常熟悉 :),我怀疑您真正需要的是:

    Dim time As Date = DateTime.ParseExact("00:01:02,123", "hh:mm:ss,fff", CultureInfo.InvariantCulture)
    Dim startTime As Date = DateTime.ParseExact("00:00:00,000", "hh:mm:ss,fff", CultureInfo.InvariantCulture)
    Dim elapsed As TimeSpan = time - startTime
    Dim totalMilliseconds As Integer = CType(elapsed.TotalMilliseconds, Integer)

您可以以同样的方式将每个字幕的开始时间和结束时间转换为总毫秒数,然后以这种方式进行比较。

正如其他人所指出的,On Error Resume Next 仅在 VB.NET 中真正可用,以向后兼容 VB6 代码。您应该改用 Try/Catch 块。但是,即使在 VB6 中,只在整个方法的上方放置一个简历也从来没有被认为是好的做法,就像在整个方法周围放置一个 try/catch 块也被认为是一个坏主意一样。

同样,GoTo 几乎是任何程序员所能做的最糟糕的事情。您应该考虑其他选项,例如循环、if/else 块、将代码分解为单独的方法等,并且不惜一切代价避免使用 GoTo。

【讨论】:

  • Tnx 兄弟!在这为什么更容易提取 evreything :),但“goto”的东西仍然很烦人:/
  • 感谢您尝试更具体地回答您的问题。你在进步!
  • 我尝试了 Do While stopWatch.Elapsed.Seconds secs Application.Doevents() 循环 - 和计时器,它仍然无法正常工作,兄弟
  • 在什么方面不起作用?它会卡在循环中吗?你不应该那样做。 DoEvents 是一个坏主意,应尽可能避免。相反,您应该使用我在上一个问题中建议的快速计时器。
  • 是的,它卡在了循环中虽然 Seconds = secs
猜你喜欢
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 2016-07-22
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2010-09-22
相关资源
最近更新 更多