How expensive is a call to ThrowIfCancellationRequested?

We have a long-running operation that read millions of records in a loop. I was wondering if I should call ThrowIfCancellationRequested in that loop (which would mean it is executed N times), or should I do it only every 1000th time?

So my question really is: how expensive is a call to ThrowIfCancellationRequested? Does it involve database reading?

Yes, it does involve database querying, but only one query is being issued:

And some deserialization stuff in SQL Server implementation:

Thanks. So in a loop, this is prohibitive. I’ll make sure, it is only being executed once every 3 secs or so for a performing UI cancelling button.

1 Like