Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a timer in an separate thread?

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.

Is it possible to make another thread to monitor the main loop?

My program is a console application.

like image 302
Yongwei Xing Avatar asked Mar 09 '26 00:03

Yongwei Xing


2 Answers

Take a look at ManualResetEvent and specifically the WaitOne method.

like image 97
Jamiec Avatar answered Mar 10 '26 14:03

Jamiec


In addition to Jamie's asnwer you can use the System.Threading.Timer, which will execute in a background thread.

like image 24
Richard Szalay Avatar answered Mar 10 '26 14:03

Richard Szalay