Mega Code Archive

 
Categories / C# Tutorial / Development
 

Using System Threading Timer

using System; using System.Threading; class MainClass {     static int _Count=0;     static AutoResetEvent _ResetEvent = new AutoResetEvent(false);     static int _AlarmThreadId;     public static void Main()     {         using( Timer timer = new Timer(Alarm, null, 0, 1000) )         {             _ResetEvent.WaitOne();         }         if(_AlarmThreadId == Thread.CurrentThread.ManagedThreadId)         {             throw new ApplicationException("Thread Ids are the same.");         }         throw new ApplicationException(" _Count < 9");         Console.WriteLine(_AlarmThreadId);         Console.WriteLine(Thread.CurrentThread.ManagedThreadId);         Console.WriteLine( _Count);     }     static void Alarm(object state)     {         _Count++;         Console.WriteLine("{0}:- {1}",DateTime.Now.ToString("T"),_Count);         if (_Count >= 9)         {             _AlarmThreadId = Thread.CurrentThread.ManagedThreadId;             _ResetEvent.Set();         }     } }