1

I understand that a Stopwatch can be useful to keep iterating a loop until X amount of time has elapsed:

void DoWork()
{
    TimeSpan maxDuration = TimeSpan.FromMinutes(3);
    Stopwatch sw = Stopwatch.StartNew();

    while (sw.Elapsed < maxDuration)
    {
        // do some work
    }
}

But what if that loop contains an action (such as a call to an external resource) which takes a long time, won't the loop wait until the response has returned, before iterating again and seeing that the StopWatch has elapsed?

void DoWork()
{
    TimeSpan maxDuration = TimeSpan.FromMinutes(3);
    Stopwatch sw = Stopwatch.StartNew();

    while (sw.Elapsed < maxDuration)
    {
        var response = CallExternalResourceWhichTakes5Minutes()
    }
}

Am I correct in that we won't check the while loop's condition again until the response has come back? If so, what would be an appropriate solution to abort the external call if the timer elapses before a response is returned?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
FBryant87
  • 4,273
  • 2
  • 44
  • 72
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 09 '14 at 13:42
  • 1
    @FBryant, your question is quite broad, there are at least two directions answers could go with (threads and async calls). Could you provide more details about your environment and the context of these timed calls? (Your question is also related to [mine](http://stackoverflow.com/questions/4503757/when-implementing-time-constrained-methods-should-i-abort-the-worker-thread-or), the answers there may be useful to you.) – Frédéric Hamidi May 09 '14 at 13:49

1 Answers1

3

Am I correct in that we won't check the while loop's condition again until the response has come back?

this is evident as you are using a sync call to method.

what would be an appropriate solution to abort the external call if the timer elapses before a response is returned

one of the appropriates way to do this is to use Task with Task.Wait Method (TimeSpan)

  var myTask= Task.Factory.StartNew(()=>CallExternalResourceWhichTakes5Minutes()); 

and here you can set your timeout the result after 5 minutes will be ignored

 Task.Wait(myTask,new TimeSpan(0,5,0)); 
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • Thank you for this KB, could you please elaborate a little on what happens once the time elapses. Does the external call continue to process, but the caller thread ignore this and carry on? – FBryant87 May 09 '14 at 14:02
  • 1
    it will simply define an internal TimeOut that will block the parent task's execution and if the timeout has reached it will return true if the Task completed execution within the allotted time; otherwise, false. this means that your code can continue the execution before 5 minutes it's reached if the external method's call was executed – BRAHIM Kamel May 09 '14 at 14:33