Timer.Stop 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會停止計時器。
public:
void Stop();
public void Stop();
member this.Stop : unit -> unit
Public Sub Stop ()
範例
以下程式碼範例實作了一個簡單的間隔計時器,每五秒觸發一次警報。 警報發生時,會 MessageBox 顯示警報啟動次數,並提示使用者計時器是否應該繼續運行。
public ref class Class1
{
private:
static System::Windows::Forms::Timer^ myTimer = gcnew System::Windows::Forms::Timer;
static int alarmCounter = 1;
static bool exitFlag = false;
// This is the method to run when the timer is raised.
static void TimerEventProcessor( Object^ /*myObject*/, EventArgs^ /*myEventArgs*/ )
{
myTimer->Stop();
// Displays a message box asking whether to continue running the timer.
if ( MessageBox::Show( "Continue running?", String::Format( "Count is: {0}", alarmCounter ), MessageBoxButtons::YesNo ) == DialogResult::Yes )
{
// Restarts the timer and increments the counter.
alarmCounter += 1;
myTimer->Enabled = true;
}
else
{
// Stops the timer.
exitFlag = true;
}
}
public:
static void Main()
{
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer->Tick += gcnew EventHandler( TimerEventProcessor );
// Sets the timer interval to 5 seconds.
myTimer->Interval = 5000;
myTimer->Start();
// Runs the timer, and raises the event.
while ( !exitFlag )
{
// Processes all the events in the queue.
Application::DoEvents();
}
}
};
int main()
{
Class1::Main();
}
public class Class1 {
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
static int alarmCounter = 1;
static bool exitFlag = false;
// This is the method to run when the timer is raised.
private static void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
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) {
// Restarts the timer and increments the counter.
alarmCounter +=1;
myTimer.Enabled = true;
}
else {
// Stops the timer.
exitFlag = true;
}
}
public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start();
// Runs the timer, and raises the event.
while(!exitFlag) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
}
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
備註
你也可以將屬性false設Enabled為 來停止計時器。 同一 Timer 物件在同一應用程式會話中可以多次啟用或停用。
在你停用 a Timer by calling Stop 後再呼叫Start,會導致中Timer斷的區間重新開始。 如果你 Timer 設定了 5000 毫秒的間隔,而你在大約 3000 毫秒時呼叫 Stop , Start 呼叫會讓事件 Timer 延遲 5000 毫秒才會升起 Tick 。
備註
在 Windows 表單應用程式中呼叫 Timer Stop 時,可能會立即處理來自應用程式其他 Timer 元件的訊息,因為所有 Timer 元件都運作在主應用程式執行緒上。 如果你有兩個Timer元件,一個設定為 700 毫秒,一個設定為 500 毫秒,而你在第一個Timer元件呼叫Stop,你的應用程式可能會先收到第二個元件的事件回撥。 如果這有問題,可以考慮改用命名空間中的System.Threading類別Timer。