English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Without further ado, let's look at the code:
using System; using System.Collections.Generic; using System.Text; namespace NET.MST.Sixth.Reenter { class Reenter { //A static member used to cause thread synchronization issues 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 reentrancy test:"); TimersTimerReenter(); //Here, it ensures that the started callback method has a chance to complete System.Threading.Thread.Sleep(2 * 1000); Console.WriteLine("System.Threading.Timer reentrancy test:"); ThreadingTimerReenter(); Console.Read(); } /// <summary> /// Demonstrates the reentrancy of the System.Timers.Timer callback method /// </summary> static void TimersTimerReenter() { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 100; //100 milliseconds timer.Elapsed += TimersTimerHandler; timer.Start(); System.Threading.Thread.Sleep(2 * 1000); //run2seconds timer.Stop(); } /// <summary> /// Demonstrates the reentrancy of the System.Threading.Timer callback method /// </summary> static void ThreadingTimerReenter() { //100 milliseconds using (System.Threading.Timer timer = new System.Threading.Timer) (new System.Threading.TimerCallback(ThreadingTimerHandler), null, 0, 100)) { System.Threading.Thread.Sleep(2 * 1000); //run2seconds } } /// <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()); //sleep10seconds, to ensure method reentrancy System.Threading.Thread.Sleep(300); TestInt1++; Console.WriteLine("Incremental1Post-test integer: " + 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()); //sleep10seconds, to ensure method reentrancy System.Threading.Thread.Sleep(300); TestInt2++; Console.WriteLine("Incremental1Post-test integer: " + TestInt2.ToString()); } } } }
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to get more support for the Yelling Tutorial!
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)