Every N minutes we want to run through a list of tasks. So we\'ve created a task executor with a
do { DoWork(); }while(!stopRequested)
No
If all your thread is doing is something like:
while (!stop_working)
{
DoWork();
Thread.Sleep(FiveMinutes);
}
Then I would suggest not using a thread at all. First, there's no particularly good reason to incur the system overhead of a dedicated thread that spends most of its time sleeping. Secondly, if you set the stop_working flag 30 seconds after the thread stops sleeping, you'll have to wait four and a half minutes before the thread wakes up and terminates.
I'd suggest as others have: use a timer:
System.Threading.Timer WorkTimer;
// Somewhere in your initialization:
WorkTimer = new System.Threading.Timer((state) =>
{
DoWork();
}, null, TimeSpan.FromMinutes(5.0), TimeSpan.FromMinutes(5.0));
And to shut down:
WorkTimer.Dispose();