elapsed.Milliseconds 獲取毫秒。
創(chuàng)新互聯(lián)是一家集網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站頁面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)絡(luò)公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。
每100毫秒,do中的代碼執(zhí)行一次,執(zhí)行時(shí)間將大于100毫秒,這是產(chǎn)生誤差的地方之一。invoke執(zhí)行一個(gè)方法txt,必須等txt里面的代碼運(yùn)行完畢,方法才能返回,期間占用的時(shí)間全是“誤差”。
無限循環(huán),且沒有退出機(jī)制,停不下來。
定時(shí)器中處理
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
Timer1.Interval?=?10?'10毫秒刷新一次
Timer1.Start()?'開啟定時(shí)器
End?Sub
Private?Sub?Timer1_Tick(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Timer1.Tick
Label1.Text?=?Now
End?Sub
不對(duì)。步驟如下:
添加一個(gè)label標(biāo)簽名字label1 用來顯示時(shí)間
再添加一個(gè)timer控件 名字timer1 interval屬性=1000 用來計(jì)時(shí)
窗體添加代碼
Dim t As Date '用來記錄時(shí)間
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Timer1.Tick
t = t.AddSeconds(1)
Label1.Text = "登錄時(shí)間:" t.TimeOfDay.ToString
End Sub
不需要控件就可以,用多線程的方式實(shí)現(xiàn),在窗體上放一個(gè)Label和一個(gè)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?=?"結(jié)束"
Start?=?DateAndTime.Timer
td?=?New?Thread(AddressOf?fun1)
td.Start()
Else
Button1.Text?=?"開始"
td.Abort()
End?If
End?Sub
'定義一個(gè)線程
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
'定義一個(gè)委托
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
給你個(gè)方法,在軟件加載時(shí)就判斷下當(dāng)前系統(tǒng)時(shí)間(最好是判斷遠(yuǎn)程服務(wù)器上的時(shí)間,因?yàn)楫?dāng)前運(yùn)行系統(tǒng)的時(shí)間他可以手動(dòng)),判斷當(dāng)前時(shí)間是否有超過最后期限。。
是我以前自己設(shè)計(jì)的用來測試自己點(diǎn)鈔速度用的,希望是你需要的
以下是窗體的全部代碼
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