Timer.Stop Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Arrête le minuteur.
public:
void Stop();
public void Stop();
member this.Stop : unit -> unit
Public Sub Stop ()
Exemples
L’exemple de code suivant implémente un minuteur d’intervalle simple, qui déclenche une alarme toutes les cinq secondes. Lorsque l’alarme se produit, un MessageBox nombre d’affichages indique le nombre de fois où l’alarme a démarré et invite l’utilisateur à déterminer si le minuteur doit continuer à s’exécuter.
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
Remarques
Vous pouvez également arrêter le minuteur en définissant la Enabled propriété sur false. Un Timer objet peut être activé et désactivé plusieurs fois dans la même session d’application.
L’appel Start après avoir désactivé un Timer appel Stop entraîne le Timer redémarrage de l’intervalle interrompu. Si votre Timer paramètre est défini pour un intervalle de 5 000 millisecondes et que vous appelez Stop à environ 3 000 millisecondes, l’appel Start entraîne l’attente Timer de 5 000 millisecondes avant de déclencher l’événement Tick .
Note
L’appel d’arrêt sur n’importe quelle Timer application Windows Forms peut entraîner le traitement immédiat des messages d’autres Timer composants de l’application, car tous les Timer composants fonctionnent sur le thread d’application principal. Si vous avez deux Timer composants, un ensemble défini sur 700 millisecondes et un sur 500 millisecondes, et vous appelez Stop le premier Timer, votre application peut recevoir un rappel d’événement pour le deuxième composant en premier. Si cela pose problème, envisagez d’utiliser la Timer classe dans l’espace de noms à la System.Threading place.