C # Implementation of program code is automatically executed at every specified time
In general projects we seldom use C # to automate code execution every specified time, but if you go through a lot of projects, or if your application does a lot more, the C # implementation automatically executes the program code more often every time. Like playing online games to do hanging machine timed advertising such projects are used in C # to implement the program code every time interval.  25 billion enterprise website management system in the operation of many years of SEO, the use of automatic code execution, and now to you to explain the C # implementation of the code every time, automatically execute the program. can be implemented in three ways in practical applications. //method One: Call the thread thread execution method, implement a dead loop in the execution method, set the time in each loop sleep thread thread25yi = new Thread (new ThreadStart (obj). MethodTimer1)); thread25yi. Start (); void MethodTimer1 () { while (True) { console.writeline (DateTime.Now.ToString () + "_" + thread25yi. CurrentThread.ManagedThreadId.ToString ()); thread25yi. Currentthread.join (100);//Block Set time } } method Two: Use System.Timers.Timer class System.Timers.Timer t25yi = new System.Timers.Timer (100);//Instantiate the Timer class with a set interval of 100 milliseconds t25yi. Elapsed + = new System.Timers.ElapsedEventHandler (obj. METHODTIMER2);//MethodTimer2 event t25yi is executed when the time of arrival is reached. AutoReset = True;//false is executed once, true is always performed t25yi. Enabled = true;//Sets whether to execute System.Timers.Timer.Elapsed event while (True) { console.writeline ("Test_" + Thread.CurrentThread.ManagedThreadId.ToString ()); thread.sleep (+); } void MethodTimer2 (object Source, System.Timers.ElapsedEventArgs E) { console.writeline (DateTime.Now.ToString () + "_" + Thread.CurrentThread.ManagedThreadId.ToString ()); } method Three: Use System.Threading.Timer System.Threading.Timer Threadtimer = new System.Threading.Timer (new System.Threading.TimerCallback (obj). METHODTIMER3), NULL, 0, while (true) { console.writeline ("Test_" + Thread.CurrentThread.ManagedThreadId.ToString ()); thread.sleep (+); } void MethodTimer3 (Object State) { console.writeline (DateTime.Now.ToString () + "_" + Thread.CurrentThread.ManagedThreadId.ToString ()); } timer constructor parameter description: callback is a TimerCallback delegate that represents the method to execute. state is an object that contains the information to be used by the callback method, or is a null reference (Nothing in Visual Basic). duetime is the amount of time (in milliseconds) to delay before calling callback, specifying Timeout.infinite to prevent the timer from starting. Specify 0 (0) to start the timer immediately. period is the time between calls to callbackInterval (in milliseconds), specifying that timeout.infinite can disable periodic termination. The above method with a timer has a disadvantage, that is, when the timer interval, the event is not completed, the timer will create a new thread, from the beginning to execute the event, and the last thread to continue to execute, this will appear, the system is more than N, perhaps a short while the system resources are exhausted.
C # Implementation automatically executes program code every specified time