I've an asynchronous method where I'm running a void-return type method in a Task and I want that method to be cancelled in X time (let's say 5 seconds) if it hasn't completed by then.
I found two similar threads: Cancel long running Task after 5 seconds when not finished Asynchronously wait for Task<T> to complete with timeout but I don't manage to get the cancellation to work using these.
This is what I've so far:
using (var cts = new CancellationTokenSource(5000))
{
await Task.Run(() =>
{
CandySystem.QueryCandies(rowOrderByIndex, columnOrderByIndex,
orderByDescending);
}, cts.Token);
}
I expect method "QueryCandies" to cancel after running for 5 seconds. Could someone provide me "for dummies" explanation what's going wrong here?