I have a blocking library call, an imap Idle that will be in a long running service waiting for email, I'm paranoid and don't trust the library to never miss an email. The Idle call can be cancelled by a concurrent call to StopIdle. I implemented the following way to every minute call StopIdle if it thinks its still idling.
Is there a better way to do the following? This methods works but it seems like I will end up taking up a bunch of thread pool threads just sleeping.
while (true)
{
// read unseen emails here ...
var cancelSource = new CancellationTokenSource();
var cancelToken = cancelSource.Token;
Task stopIdleOccasionally = Task.Factory.StartNew(() =>
{
Thread.Sleep(TimeSpan.FromMinutes(1));
if (cancelToken.IsCancellationRequested)
{
return;
}
client.StopIdle(); // This causes the Idle() call to return
},
cancelSource.Token);
client.Idle(); // This is the blocking call that I want to create a timeout for
cancelSource.Cancel();
}