So the timing of the implementation of the task, multi-digital farming will certainly know the timer, and there are various statements.
Three timer classes in C #:
System.Timers.Timer: Triggers and executes code at a fixed interval, which is used primarily on the server side or as a serviced component in a multithreaded environment, and it has no user interface and is not visible at run time. deprecated in. NET Framework 5,. NET core and ASP.
System.Threading.Timer: Executes a callback method at a fixed interval in a thread pool that is defined at instantiation and cannot be modified. Like System.Timers.Timerg. Used primarily for server or as a service component in a multithreaded environment, it has no user interface and is not visible at run time. Recommended
System.Windows.Forms.Timer: is a component of Windows Forms that triggers an event and executes at regular intervals, which does not have a user interface and is designed for a single thread .
System.Web.UI.Timer: is an ASP. NET component that can be used for recurring asynchronous or synchronous Web page postbacks.
In short, first to these three kinds of timer actual combat once:
System.Timers.Timer, paste code:
public class timertest{ private system.timers.timer timer = new timer (); private datetime dtswitchjob = null; public timertest () { timer. interval = 1000;//executes timer once per second. Elapsed += t_elapsed; } public void start () { timer. enabled = true; dtswitchjob = DateTime.Now.AddMinutes (1);//After one minute modify the execution interval } public void stop () { Timer. Enabled = falsE; } private void t_elapsed ( Object sender, elapsedeventargs e) { datetime dtnow = datetime.now; if (Dtnow.date == dtswitchjob.date && dtnow.hour == dtswitchjob.hour && dtnow.minute == dtswitchjob.minute) { timer. interval = 60*1000;//set to execute once per minute } console.writeline (DateTime.Now); }}
system.threading.timer , post code:
private static system.threading.timer timer = null; //new System.Threading.Timer (run, null, , 2000);p Ublic static void run (object param) { console.writeline ("Running" + datetime.now); Timer. Change (10 * 1000, timeout.infinite);//execute guan }public timertest () { every 10 seconds timer = new system.threading.timer (run, null, 1000, 5000);//1 seconds later, Executes once every 5 seconds}//The following is an example of a timer with parameters, in order not to fraught I directly stick to the official:using system;using system.threading;class Timerexample{ static void main () { //Create an event in the callback function to identify the threshold value of the timeout quantity Autoresetevent autoevent = new autoresetevent (False); statuschecker statuschecker&nbSp;= new statuschecker ( //); Create an inference delegate for a timer for a timer TimerCallback tcb = statusChecker.CheckStatus; //create an identity delegate after 1 seconds, call the checkstatus timer every 1/4 seconds console.writeline ("{0} creating timer.\n", DateTime.Now.ToString ( "H:mm:ss.fff")); timer statetimer = new Timer (tcb, autoevent, 1000, 250); // When Autoevent is identified, the change cycle is once every 1/2 minutes autoevent.waitone (5000, false) ; statetimer.change (0, 500); console.writeline ("\nchanging period.\n"); //when Autoevent is identified for the second time, release TIMER&NBSp; autoevent.waitone (5000, false); statetimer.dispose (); console.writeline ("\ndestroying timer."); }}class StatusChecker{ private int invokeCount; private int maxcount; public statuschecker (Int count) { invokeCount = 0; maxCount = count; } //This method will be called by the timer delegate public void checkstatus ( Object stateinfo) { autoresetevent autoEvent = (AutoResetEvent) Stateinfo; consolE.writeline ("{0} checking status {1,2}.", datetime.now.tostring ("H:mm:ss.fff"), (+ + Invokecount). ToString ()); if (Invokecount == maxcount) { / /reset counter and give the main thread a signal invokeCount = 0; autoevent.set (); } }}
Add:
AutoResetEvent allows threads to communicate with each other by signaling. Typically, this communication involves a resource that the thread requires exclusive access to.
The thread waits for a signal by calling WaitOne on autoresetevent . If the AutoResetEvent is in a non-terminating state, the thread blocks and waits for the current thread that controls the resource
Sends a signal that the resource is available by calling Set .
Call Set to signal AutoResetEvent to release the waiting thread. AutoResetEvent will remain signaled until a waiting thread is freed and then automatically returned to the non-terminating state. If no thread is waiting, the state remains in the terminating state indefinitely.
You can control the initial state of the AutoResetEvent by passing a Boolean value to the constructor, trueif the initial state is signaled, or false.
In layman's terms, only after the successful operation of the Myreseteven.set (), Myreseteven.waitone () will be able to obtain operational opportunities; Set is the signal, WaitOne is waiting for the signal, only sent a signal,
Wait for it to execute. If not, the program behind WaitOne will never be executed.
This supplement is from: http://www.cnblogs.com/lzjsky/archive/2011/07/11/2102794.html
system.windows.forms.timer , post code:
Drag a timer in the toolbox. This will generate the code snippet in the section #region the form Design section without looking at partial class form1{ /// <summary> /// Required designer variable. /// </summary> private System.componentmodel.icontainer components = null; /// <summary > /// Clean up any resources being used. /// </summary> /// <param name= "disposing" >true if managed resources should be disposed; otherwise, false.</param > protected override void dispose (bool disposing) { if (disposing && ( Components != null)) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; { components. Dispose (); } Base. Dispose (disposing); } #region Windows Form designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent () { this.components = new system.componentmodel.container (); This.timer1 = new system.windows. Forms.timer (this.components); this. SuspendLayout (); // // timer1 // this.timer1.tick += new system.eventhandler (This.timer1_Tick); // // form1 // This. Autoscaledimensions = new system.drawing.sizef (8f, 15f); this. autoscalemode = system.windows.forms.autoscalemode.font; this. Clientsize = new system.drawing.size (646, 455); this. Name = "Form1"; this. text = "Form1"; this. Load += new system.eventhandler (this. Form1_Load); this. ResumeLayout (False); } #endregion private system.windows.forms.timer timer1;} #endregion//Note that the following line of event binding code: This.timer1.tick += new system.eventhandler (This.timer1_tick);// The timer1 is set in the initialization or load event, or directly in the form. interval = 1000;//per second//below is the body part of the method you want to execute Private void timer1_tick (Object sender, eventargs e) { console.writeline ("I'll come back!") ");}
This article refers to:
Official explanation of the timer: https://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
Discussion of feeling help: Http://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer
This man also has an example: http://www.cnblogs.com/Joetao/articles/2643378.html
C # 's Timer combat