十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
elapsed.Milliseconds 获取毫秒。
在浈江等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、成都做网站 网站设计制作按需策划设计,公司网站建设,企业网站建设,品牌网站制作,成都营销网站建设,外贸网站制作,浈江网站建设费用合理。
每100毫秒,do中的代码执行一次,执行时间将大于100毫秒,这是产生误差的地方之一。invoke执行一个方法txt,必须等txt里面的代码运行完毕,方法才能返回,期间占用的时间全是“误差”。
无限循环,且没有退出机制,停不下来。
format是肯定不能带毫秒滴。要实现你的效果,也简单
Format(time,"YYYY-MM-DD HH:MM:SS") "." ( timer()*1000 mod 1000)
Format(time,"YYYY-MM-DD HH:MM:SS") 加上 . 再加上毫秒数,就构成了
timer()代表的是,从午夜到现在,所经过的秒数,它的小数部分x1000就是毫秒
试过NN次后,我“也”发现这种记录时间的精度为10.0144毫秒,
看来,这个问题是由CLR决定的,如果,只使用FCL的函数,看来没办法解决这一问题了!
看了 LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^) ,给出的文章链接,
受了些启发,对于文章中实现的那个A类,我没看太明白,但是,文章中用到了QueryPerformanceCounterp这个win API函数,楼主,可以导入用一下,我试过多次,在我的机子上,其精度为 210/1000 毫秒左右。
QueryPerformanceCounter这个函数的是一个计数器函数,以百万分之一秒为单位,来记算时间数。
//=============
using System.Runtime.InteropServices;
using System.Threading;
[DllImport("kernel32.dll")]
static extern bool QueryPerformanceCounter([In, Out] ref long lpPerformanceCount);
long t1 = 0;
long t2 = 0;
QueryPerformanceCounter(ref t1);
Thread.Sleep(1); //挂起一毫秒,作为演示
QueryPerformanceCounter(ref t2);
long sp = t2 - t1;//sp的单位为百万分之一秒
float f_time = (float)((decimal) sec / (decimal)10000); //得到0.0001毫秒的精度
f_time = float.Parse(string.Format("{0:F1}",f_time)); //f_time是经过四舍五入,得到的0.1毫秒的精度
不需要控件就可以,用多线程的方式实现,在窗体上放一个Label和一个button按钮,代码如下:
Imports System.Threading
Public Class Form1
Dim Start As Double
Dim td As Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = "00:00:00 000"
Button1.Text = "开始"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "开始" Then
Button1.Text = "结束"
Start = DateAndTime.Timer
td = New Thread(AddressOf fun1)
td.Start()
Else
Button1.Text = "开始"
td.Abort()
End If
End Sub
'定义一个线程
Private Sub fun1()
Dim elapsed As TimeSpan
Dim Dg_txt As New Dg(AddressOf txt)
Do
elapsed = TimeSpan.FromMilliseconds((DateAndTime.Timer - Start) * 1000)
Me.Invoke(Dg_txt, elapsed.ToString, Label1)
Thread.Sleep(100)
Loop
End Sub
'定义一个委托
Delegate Sub Dg(ByVal s As String, ByVal obj As Label)
Sub txt(ByVal s As String, ByVal obj As Label)
obj.Text = s
End Sub
End Class
dim a1 as timespan = dtEndTime -dtStartTime
dim a2 as integer = a1.TotalMilliseconds
是我以前自己设计的用来测试自己点钞速度用的,希望是你需要的
以下是窗体的全部代码
Public Class Form1
Dim StartFlag As Boolean = False
Dim secon As Integer
Dim minut As Integer
'空格
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Space Then
If StartFlag Then
StartFlag = False
Timer1.Enabled = False
If Val(Strings.Right(Label1.Text, 2)) 10 And Val(Strings.Right(Label1.Text, 2)) = 0 Then secon = 0 : minut = 0 : Label1.Text = "00:00" : Exit Sub
ListBox1.Items.Add(Label1.Text.ToString)
ListBox1.SelectedItem = ListBox1.Items.Count - 1
Label1.Focus()
Button1.Enabled = True
Label1.Text = "00:00"
secon = 0
minut = 0
Else
StartFlag = True
Timer1.Enabled = True
End If
End If
End Sub
'加载
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
Label1.Text = "00:00"
Button1.Enabled = False
secon = 0
minut = 0
Label1.Focus()
End Sub
'清空
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Enabled = False
ListBox1.Items.Clear()
Label1.Focus()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
secon += 1
If secon = 60 Then
secon = 0
minut += 1
End If
Dim seconStr As String = secon
If seconStr.Length = 1 Then seconStr = "0" + seconStr
Dim minutStr As String = minut
If minutStr.Length = 1 Then minutStr = "0" + minutStr
Label1.Text = minutStr + ":" + seconStr
Label1.Focus()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SeconSun As Integer
If ListBox1.Items.Count 0 Then
For i = 0 To ListBox1.Items.Count - 1
Dim TemStr As String = ListBox1.Items.Item(i).ToString
Dim TemInt1 As Integer = Val(Strings.Right(TemStr, 2))
Dim TemInt2 As Integer = Val(Strings.Left(TemStr, 2))
Debug.Print(TemInt1.ToString)
Debug.Print(TemInt2.ToString)
SeconSun += TemInt1 + TemInt2 * 60
Debug.Print(SeconSun.ToString)
Next
TextBox1.Text = (SeconSun / ListBox1.Items.Count).ToString + "秒"
End If
Label1.Focus()
End Sub
End Class