ASP. NET timer callback method, asp.net timer callback
For more information, see the code:
Using System; using System. collections. generic; using System. text; namespace NET. MST. sixth. reenter {class Reenter {// static member used to cause thread synchronization problems private static int TestInt1 = 0; private static int TestInt2 = 0; private static object locko = new object (); static void Main (string [] args) {Console. writeLine ("System. timers. timer callback method re-import test: "); TimersTimerReenter (); // make sure that the started callback method has the opportunity to end the System. threading. thread. sleep (2*1000); Console. writeLine ("System. threading. timer callback method re-import test: "); ThreadingTimerReenter (); Console. read () ;}/// <summary> /// display System. timers. timer callback method re-import // </summary> static void TimersTimerReenter () {System. timers. timer timer = new System. timers. timer (); timer. interval = 100; // 100 ms timer. elapsed + = TimersTimerHandler; timer. start (); System. threading. thread. sleep (2*1000); // run the timer for 2 seconds. stop () ;}/// <summary> /// display System. threading. timer callback method re-import // </summary> static void ThreadingTimerReenter () {// 100 ms using (System. threading. timer timer = new System. threading. timer (new System. threading. timerCallback (ThreadingTimerHandler), null, 0,100) {System. threading. thread. sleep (2*1000); // run for 2 seconds }/// <summary> // System. timers. timer callback method /// </summary> /// <param name = "sender"> </param> /// <param name = "args"> </param> private static void TimersTimerHandler (object sender, eventArgs args) {lock (locko) {Console. writeLine ("test INTEGER:" + TestInt1.ToString (); // sleep for 10 seconds. Ensure that the method is reimported to System. threading. thread. sleep (300); TestInt1 ++; Console. writeLine ("test integer after auto-increment 1:" + TestInt1.ToString () ;}/// <summary> // System. threading. timer callback method // </summary> // <param name = "state"> </param> private static void ThreadingTimerHandler (object state) {lock (locko) {Console. writeLine ("test INTEGER:" + TestInt2.ToString (); // sleep for 10 seconds, ensure that the method is reentrant to System. threading. thread. sleep (300); TestInt2 ++; Console. writeLine ("test integer after auto-increment 1:" + TestInt2.ToString ());}}}}
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!