Private Timer loginTimer; // defines a Timer. In this case, you need to introduce the namespace: using System. Timers;
Private int loginCouter; // logon count counter
Private TimerTest ()
{
If (false) // This is pseudo code
{
// Login Failed
}
This. loginCouter = 0; // The number of logons reaches 0.
This. loginTimer = new System. Timers. Timer () // set attributes for the Timer
{
AutoReset = false, // automatically update to false
Interval = 200 // restart the timer once in 200 milliseconds
};
This. loginTimer. Elapsed + = new ElapsedEventHandler (loginTimer_Elapsed); // registers the event to be executed by the timer
This. loginTimer. Start (); // Start the timer.
}
Void loginTimer_Elapsed (object sender, ElapsedEventArgs e) // internal event write function
{
This. loginCouter ++;
If (LOGIN successful) // This is pseudo code
{
This. Dispatcher. BeginInvoke (new Action () => {
This. ConsoleTextBox. Text = "Login successful! \ N ";
}), Null); // The purpose of this write is to start a thread and other threads separately (using multiple threads) to run the timer, so that the program will not become stuck due to a single thread.
}
Else if (this. loginCouter> 150)
{
This. Dispatcher. BeginInvoke (new Action () =>
{
This. ConsoleTextBox. Text = "Login Request timeout! \ N ";
}), Null );
}
Else
{
This. loginTimer. Start (); // restart the timer.
}
}
Author: haitaoDoit