ASP. NET timer callback method, asp.net timer callback

Source: Internet
Author: User

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!

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.