Re-entry of the ASP. NET Timer callback method

Source: Internet
Author: User
If you don't say much, look at the code:







Using System;
Using System. Collections. Generic;
Using System. The Text;
The namespace NET. The MST. Sixth. Reenter
{
The 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();
The static void Main (string [] args)
{
Console.WriteLine(" system.timer. Timer callback method reentry test: ");
TimersTimerReenter ();
// here make sure that callback methods that have already started have a chance to end
System. Threading. Thread. Sleep (2 * 1000);
Console.writeline (" system.threading.timer callback method reentry test: ");
ThreadingTimerReenter ();
The Console. The Read ();
}
/ / / < summary >
/// shows the callback method reentrant of system.timer.timer
/ / / < / summary >
The static void TimersTimerReenter ()
{
Timer = new system.timer.timer ();
The timer. The Interval = 100; / / 100 milliseconds
The timer. Elapsed + = TimersTimerHandler;
The timer. The Start ();
System. Threading. Thread. Sleep (2 * 1000); // run for 2 seconds
The timer. Stop ();
}
/ / / < summary >
/// shows the callback method reentrancy of system.threading.timer
/ / / < / summary >
The static void ThreadingTimerReenter ()
{
/ / 100 milliseconds
Using (system.threading.timer Timer = new system.threading.timer
(the new System. Threading. TimerCallback (ThreadingTimerHandler), null, 0, 100))
{
System. Threading. Thread. Sleep (2 * 1000); // run for 2 seconds
}
}
/ / / < summary >
/// callback method of system.timer.timer
/ / / < / summary >
/ / / < param name = "sender" > < param >
/ / / < param name = "args" > < param >
Private static void TimersTimerHandler(object sender,EventArgs args)
{
The lock (locko)
{
Console.WriteLine(" test integer: "+ testint1.tostring ());
// sleep for 10 seconds to ensure reentry
System. Threading. Thread. Sleep (300);
TestInt1 + +;
Console.WriteLine(" test integer after incrementing 1: "+ testint1.tostring ());
}
}
/ / / < summary >
/// callback method for system.threading.timer
/ / / < / summary >
/ / / < param name = "state" > < param >
Private static void ThreadingTimerHandler(object state)
{
The lock (locko)
{
Console.WriteLine(" test integer: "+ testint2.tostring ());
// sleep for 10 seconds to ensure reentry
System. Threading. Thread. Sleep (300);
TestInt2 + +;
Console.WriteLine(" test integer after incrementing 1: "+ testint2.tostring ());
}
}
}
} 
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.