Timer.Stop Metoda
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Zatrzymuje czasomierz.
public:
void Stop();
public void Stop();
member this.Stop : unit -> unit
Public Sub Stop ()
Przykłady
Poniższy przykład kodu implementuje prosty czasomierz interwału, który uruchamia alarm co pięć sekund. Po wystąpieniu alarmu wyświetlany jest licznik liczby uruchomień alarmu i monituje użytkownika o to, MessageBox czy czasomierz powinien nadal działać.
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
Uwagi
Możesz również zatrzymać czasomierz, ustawiając Enabled właściwość na false. Obiekt Timer może być włączony i wyłączony wiele razy w ramach tej samej sesji aplikacji.
Wywołanie Start po wyłączeniu Timer wywołania Stop spowoduje Timer ponowne uruchomienie przerwanego interwału. Timer Jeśli parametr jest ustawiony dla interwału 5000 milisekund i wywołasz około Stop 3000 milisekund, wywołanie wywołania Start spowoduje, że przed podniesieniem Tick zdarzenia będzie czekać Timer 5000 milisekund.
Uwaga
Wywołanie polecenia Zatrzymaj w dowolnej Timer aplikacji Windows Forms może spowodować natychmiastowe przetworzenie komunikatów z innych Timer składników aplikacji, ponieważ wszystkie Timer składniki działają w wątku głównym aplikacji. Jeśli masz dwa Timer składniki, jeden ustawiony na 700 milisekund i jeden ustawiony na 500 milisekund, a wywołanie Stop pierwszego Timerskładnika może otrzymać wywołanie zwrotne zdarzeń dla drugiego składnika. Jeśli okaże się to problematyczne, rozważ użycie Timer klasy w System.Threading przestrzeni nazw.