Without further ado, please see the code:
using System; using ; using ; namespace { class Reenter { // Static members 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) { ("Callback method reentry test:"); TimersTimerReenter(); // Here we make sure that the callback method that has already started has a chance to end (2 * 1000); ("Callback method reentry test:"); ThreadingTimerReenter(); (); } /// <summary> /// Re-enter the displayed callback method /// </summary> static void TimersTimerReenter() { timer = new (); = 100; //100ms += TimersTimerHandler; (); (2 * 1000); //Run for 2 seconds (); } /// <summary> /// Re-enter the displayed callback method /// </summary> static void ThreadingTimerReenter() { //100ms using ( timer = new (new (ThreadingTimerHandler), null, 0, 100)) { (2 * 1000); //Run for 2 seconds } } /// <summary> /// callback method /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void TimersTimerHandler(object sender,EventArgs args) { lock (locko) { ("Test integer:" + ()); //Sleep for 10 seconds to ensure that the method is re-entered (300); TestInt1++; ("Test integers after increasing by 1:" + ()); } } /// <summary> /// callback method /// </summary> /// <param name="state"></param> private static void ThreadingTimerHandler(object state) { lock (locko) { ("Test integer:" + ()); //Sleep for 10 seconds to ensure that the method is re-entered (300); TestInt2++; ("Test integers after increasing by 1:" + ()); } } } }
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!