2011-01-05 90 views

回答

4

查看Timer類。

Public Class Form1 
    Private T As Timer 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     T = New Timer() 
     AddHandler T.Tick, AddressOf TimerTicker 
     T.Interval = (1000 * 3) 'Every 3 seonds 
     T.Start() 
    End Sub 
    Private Sub TimerTicker(ByVal sender As Object, ByVal ev As EventArgs) 
     Trace.WriteLine("here") 
    End Sub 
End Class 
+0

謝謝。有用。 – Voldemort 2011-01-06 00:43:31

0

你是說在某個時間間隔運行一個函數嗎?如果是這樣,那麼定時器控制將工作。快速Google search會給你一些定時器教程。

0

這個怎麼樣:使用定時器,,只是用你想要的任何方法替換MessageBox警報。

以下示例實現了一個簡單的間隔計時器,該計時器每五秒鐘發出一次警報。發生警報時,MessageBox將顯示警報已啓動的次數並提示用戶是否應繼續運行計時器。

您可以找到更多詳細信息here

Public Class Class1 
>  Private Shared WithEvents myTimer As New System.Windows.Forms.Timer() 
>  Private Shared alarmCounter As Integer = 1 
>  Private Shared exitFlag As Boolean = False  

> 
>  ' This is the method to run when the timer is raised. 
>  Private Shared Sub TimerEventProcessor(myObject As 
> Object, _ 
>           ByVal myEventArgs As EventArgs) _ 
>          Handles myTimer.Tick 
>   myTimer.Stop() 
> 
>   ' Displays a message box asking whether to continue running the 
> timer. 
>   If MessageBox.Show("Continue running?", "Count is: " & 
> alarmCounter, _ 
>        MessageBoxButtons.YesNo) = 
> DialogResult.Yes Then 
>    ' Restarts the timer and increments the counter. 
>    alarmCounter += 1 
>    myTimer.Enabled = True 
>   Else 
>    ' Stops the timer. 
>    exitFlag = True 
>   End If 
>  End Sub 
> 
>  Public Shared Sub Main() 
>   ' Adds the event and the event handler for the method that will 
>   ' process the timer event to the timer. 
> 
>   ' Sets the timer interval to 5 seconds. 
>   myTimer.Interval = 5000 
>   myTimer.Start() 
> 
>   ' Runs the timer, and raises the event. 
>   While exitFlag = False 
>    ' Processes all the events in the queue. 
>    Application.DoEvents() 
>   End While 
> 
>  End Sub  
> 
> End Class