我使用Visual Basic 2008 編寫
專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)宣威免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
1、新建2個(gè)窗體Form1和Form2
2、Form1窗體新建一個(gè)Button按扭和一個(gè)Timer1控件
3、打開Form1編寫如下代碼
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Form2.Show()
Me.Hide() '隱藏本窗體
Timer1.Enabled = False '使其只執(zhí)行1次
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True '能使用 其實(shí)這個(gè)在屬性窗口中更容易設(shè)置
Timer1.Interval = 2000 '毫秒 即2秒
End Sub
End Class
是我以前自己設(shè)計(jì)的用來測(cè)試自己點(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
不需要控件就可以,用多線程的方式實(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
elapsed.Milliseconds 獲取毫秒。
每100毫秒,do中的代碼執(zhí)行一次,執(zhí)行時(shí)間將大于100毫秒,這是產(chǎn)生誤差的地方之一。invoke執(zhí)行一個(gè)方法txt,必須等txt里面的代碼運(yùn)行完畢,方法才能返回,期間占用的時(shí)間全是“誤差”。
無(wú)限循環(huán),且沒有退出機(jī)制,停不下來。