1

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();
}
BrandonAGr
  • 5,827
  • 5
  • 47
  • 72
  • 1
    Better to use a timeout insdie Idle or a Timer to call your StopIdle. Avoid Sleep() if you can. – H H Jul 07 '11 at 16:49
  • There is a solution here: http://stackoverflow.com/questions/11831844/unobservedtaskexception-being-throw-but-it-is-handled-by-a-taskscheduler-unobser?lq=1 –  Nov 22 '12 at 17:38

1 Answers1

1

Using Henk's suggestion I rewrote it using a timer to be:

Timer stopIdleTimeoutTimer = new Timer(
    _ => client.StopIdle(),
    null,
    TimeSpan.FromMinutes(1),
    TimeSpan.FromMilliseconds(-1));

client.Idle();

stopIdleTimeoutTimer.Dispose();

Since its a library that I don't have access to the code I can't alter the behavior of the Idle method

BrandonAGr
  • 5,827
  • 5
  • 47
  • 72