Tuesday, December 15, 2009

Run timer in another Thread

        {
           
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
           
var timer = new System.Timers.Timer(10000);
            timer
.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer
.Start();
           
for(int i=0;i<20;i++)
           
{
               
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + " " + "current I is " + i.ToString());
               
Thread.Sleep(1000);
           
}
           
Console.ReadLine();
       
}

       
static void timer_Elapsed(object sender, ElapsedEventArgs e)
       
{
           
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId+"  current is timer");
           
//throw new NotImplementedException();
       
}

I have a loop like below

for(int i = 0; i < 10; i++)

{

   
// some long time processing

}



I want to create a timer, which would check if one processing runs more than 5 minutes. If one processing runs more than 5 minutes, it would stop current processing then start another processing.


Below is one solution:

        static void Main(string[] args)

No comments:

Post a Comment