Timer, javatimer
There are three timer classes
1. System. Windows. Forms. Timer
Usage: Windows Forms Application, and must be used in the window.
2. System. Timers. Timer
Usage: In the application
One is the execution method:
1. System. Threading. Timer
Usage:The execution method mechanism cannot inherit this class.
System. Windows. Forms. timer usage
1. System. Windows. Forms. Timer myTimer = new System. Windows. Forms. Timer (); // instantiate a timer
2. myTimer. Tick + = new EventHandler (function name); // suspend the timer event
3. myTimer. Enabled = true; // make timer available
4. myTimer. Interval = n; // set the Interval, in milliseconds
5. myTimer. Stop (); // use the Stop () method to pause timing.
6. myTimer. Enabled = false; // disable timer.
Usage of System. Timers. Timer:
1 System. timers. timer t = new System. timers. timer (10000); // instantiate the Timer class, set the interval to 10000 milliseconds; 2 t. elapsed + = new System. timers. elapsedEventHandler (theout); // execute the event when the arrival time is reached; 3 t. autoReset = true; // set whether to execute once (false) or always execute (true); 4 t. enabled = true; // whether to execute System. timers. timer. elapsed event; 5 6 public void theout (object source, System. timers. elapsedEventArgs e) 7 {8 MessageBox. show ("OK! "); 9}
Usage of System. Threading. Timer:
Demo application:
Application Scenario: After the windows form program automatically executes a job, it is expected that its windows form will be automatically disabled.
Code Design: (1) Declare the Timer variable first:
Private System. Threading. Timer timerClose;
(2) Add the following Timer instantiation code after the above Automatic Execution Code:
// Create a timer thread and start it
TimerClose = new System. Threading. Timer (new TimerCallback (timerCall), this, 5000, 0 );
Description of Timer constructor parameters:
Callback: A TimerCallback delegate that indicates the method to be executed.
State: an object that contains the information to be used by the callback method, or a null reference (Nothing in Visual Basic ).
DueTime: The amount of time delay before calling callback (in milliseconds ). Specify Timeout. Infinite to prevent the timer from starting timing. Specify zero (0) to start the timer immediately.
Period: The interval (in milliseconds) between calls to callback ). The specified Timeout. Infinite can disable periodic termination.
(3) define the method to be executed by the TimerCallback delegate:
Private void timerCall (object obj)
{
TimerClose. Dispose ();
This. Close ();
}
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Threading;10 11 namespace MYTimerTest12 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void Form1_Load(object sender, EventArgs e)21 {22 23 System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(timer_Elapsed), null, 0, 1000);24 }25 26 void timer_Elapsed(object sender)27 {28 for (int i = 0; i < 10; i++)29 {30 Console.Out.WriteLine(DateTime.Now + " " + DateTime.Now.Millisecond.ToString() + "timer in:");31 }32 33 }34 }35 }