C # Write timer (do not drag controls)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.