1 year ago

#381391

test-img

Czeshirecat

C#. CancellationToken. How do I test them

C#, Visual Studio. Pardon me asking, I'm painfully new at TPL but comfortable in using BackgroundWorker in previous applications.

My app is a windows service app with a worker thread that loops. Each loop reads a batch of records from a table then creates an asynchronous task for each one of them. Each thread performs a database query that can take an average of 11 secs) to process.

I'm concerned about the "CancellationToken stoppingtoken" that's passed to my worker. I've not found much about it when I've googled, that's written in simple language.

Firstly, am I correct in assuming that it's set when the service is told to shut down?

Secondly, how do I set the stoppingtoken so I can test the effect and make my software robust?

Thirdly, are there any code samples to show how to run a query asynchronously, but interrupt/cancel a long running one so that my service can shut down quickly in response to a cancellationtoken being set? This is my biggest concern as I'm struggling to learn so much new stuff at once.

asynchronous

cancellation-token

0 Answers

Your Answer

Accepted video resources