.net - How can I cancel Task.WhenAll? -
currenly using following code wait collection of tasks complete. however, have situation want able cancel/abort whenall call, via cancellation token preferably. how go that?
dim taskcollection new list(of tasks.task) x integer = 1 threads dim newtask tasks.task = taskhandler.delegates(delegatekey).invoke(me, proxies, totalparams).continuewith(sub() threadfinished()) taskcollection.add(newtask) next await tasks.task.whenall(taskcollection)
i'm assuming it's going along lines of next bit of code, i'm not sure go in 'xxx'.
await tasks.task.whenany(tasks.task.whenall(taskcollection), xxx)
use taskcompletionsource<t>
create task asynchronous condition not have asynchronous api. use cancellationtoken.register
hook modern cancellationtoken-based cancellation system cancellation system. solution needs combine these two.
i have cancellationtoken.astask()
extension method in asyncex library, can write own such:
<system.runtime.compilerservices.extension> _ public shared function astask(cancellationtoken cancellationtoken) task dim tcs = new taskcompletionsource(of object)() cancellationtoken.register(function() tcs.trysetcanceled(), usesynchronizationcontext := false) return tcs.task end function
usage expected:
await task.whenany(task.whenall(taskcollection), cancellationtoken.astask())
Comments
Post a Comment