0

enter image description here

The above image shows when ToListAsync(cancellationToken) was called, when the token was set to cancel status, and when the cancel exception is finally thrown (6.7 seconds later).

Is this normal behavior? I need it to be faster. The MSDN Documentation says extremely little on the matter. It is a long running query, however this answer suggests that ToListAsyc(cancellationToken) should exit the query no problem. What is actually going on behind the scenes here?

There isn't any code to show unless requested, it's working... it's just taking an oddly long time.

2
  • 2
    Why do you need it to happen sooner? Why can't you just assume that cancellation will occur and roll on? It's asynchronous, after all.
    – spender
    Oct 17, 2014 at 21:43
  • That felt like bad design, for instance always getting the data and then depending on some flag, throw it away or keep it. Whats the point of the cancellation token at all if I'm essentially using that same pattern anyways?
    – ryand
    Oct 20, 2014 at 16:51

1 Answer 1

3

Is this QueryableExtensions.ToListAsync()? Or something similar?

If so, then it will all depend on the data source. If everything's in-memory, and there are no projections or other queries in the chain, then I'd expect a faster response time. But if the query is going out to some external resource or there's some time-consuming processing embedded, the operation isn't going to be able to be interrupted safely until it reaches a good spot, which could take awhile.

3
  • Yea this is a full on SQL Server call via Entity Framework, should have made that more clear.
    – ryand
    Oct 17, 2014 at 21:38
  • 2
    Well, if you're querying SQL server, it doesn't seem unreasonable that it could take seven seconds before the query gets to a point where it can check and respond to the cancellation token. Oct 17, 2014 at 21:41
  • Once EF has started retrieving results from Sql Server, it cannot always be stopped so quickly. See this answer: stackoverflow.com/a/18088174/81317
    – Rob Kent
    Sep 9, 2015 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.