【问题标题】:VB.NET Timer Interval 1 = 1 millisecond?VB.NET 定时器间隔 1 = 1 毫秒?
【发布时间】:2012-06-14 10:55:24
【问题描述】:

我有一个间隔 = 1 的计时器

间隔 1 = 1 毫秒?

如果间隔 1 不是 1 毫秒,
那么告诉我哪个控制间隔 = 1ms

代码:

Imports System.Globalization

Public Class Form1
    'Default Time To Start From
    Dim time As String = "00:00:00,000" 'Start From Here

    'Label
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Timer1.Start() 'Run The Timer On Click
    End Sub

    'TIMER
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        'Timer Interval = 1

        Dim ci = CultureInfo.InvariantCulture

        Dim original As TimeSpan = TimeSpan.ParseExact(time, "hh\:mm\:ss\,fff", ci) 'ParseExact

        Dim difference As TimeSpan = TimeSpan.FromMilliseconds(1) ' = 1  Millisecond

        Dim final = original

        final = original + difference ' connect between original to difference !(00:00:00,000 + 1 MS = 00:00:00,001)!

        Dim output As String = final.ToString("hh\:mm\:ss\,fff", ci) 'convert to the format (  = 00:00:00,001  ) (Back It To The Right Format)

        time = output '!!Update!! the Time String from 00:00:00,000 To 00:00:00,001 |||| And in the Next Time 00:00:00,001 + 1 = 00:00:00,002

        Label1.Text = time 'Show the Time String in the label

    End Sub

End Class

如您所见 - 我正在使用常规计时器,间隔为 1,
但我认为它磨损了,因为计时器不计算毫秒

如果你有建议,请告诉我。

【问题讨论】:

  • From MSDNThe Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.Timers namespace.
  • 你有 System.Timers 的例子吗?我需要计时器(1ms)
  • 向我们展示创建计时器并开始计时的代码。它可能在Main。它将包括类似Timer1.Interval = 1;
  • 或者不要打扰,因为正如史蒂夫所说,如果你真的需要每毫秒一个事件,这种计时器对你没有好处。我相当肯定System.Timers 不会超过 15 毫秒,因此也不能满足您的需求。您可能想查看stackoverflow.com/questions/3744032/… 了解更多信息。

标签: vb.net timer milliseconds


【解决方案1】:

MSDN 对区间属性的描述是:

获取或设置 Tick 事件发生之前的时间,以毫秒为单位 相对于最后一次发生的 Tick 事件引发。

但是(正如史蒂夫在评论中指出的那样):

Windows 窗体计时器组件是单线程的,并且受到限制 精度为 55 毫秒。如果您需要多线程 更准确的计时器,请使用 System.Timers 中的 Timer 类 命名空间

取自http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

这里描述了System.Timers.Timer 类:http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

【讨论】:

  • @Steve:抱歉 - 直到我发布这篇文章后才看到你的评论(不在屏幕顶部!)。有功劳。
【解决方案2】:
    Imports System.Globalization


Public Class Form1
    Dim time As String = "00:00:00:00"
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Dim ci = CultureInfo.InvariantCulture

        Dim original As TimeSpan = TimeSpan.ParseExact(time, "dd\:hh\:mm\:ss", ci) 'ParseExact

        Dim difference As TimeSpan = TimeSpan.FromSeconds(1) ' = 1  Millisecond

        Dim final = original

        final = original + difference ' connect between original to difference !(00:00:00,000 + 1 MS = 00:00:00,001)!

        Dim output As String = final.ToString("dd\:hh\:mm\:ss", ci) 'convert to the format (  = 00:00:00,001  ) (Back It To The Right Format)

        time = output '!!Update!! the Time String from 00:00:00,000 To 00:00:00,001 |||| And in the Next Time 00:00:00,001 + 1 = 00:00:00,002

        Label1.Text = time 'Show the Time String in the label

    End Sub
End Class

这是工作脚本。 IDK 为什么它有效,但它有效! :)

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多