c# - Good way for a CancellationToken to affect a subset of tasks controlled by another CancellationToken -


is there ways tie cancellation token subset of tasks controlled cancellation token?... in example create 2 cancellationtokensource's... , register 1 against cancel function of other... way if cancel loading of images... cancels tasks... if cancel all... cancels tasks... there better way this? (this ways seems messy... e.g. when unregister)

cancellationtokensource cancelallsource = new cancellationtokensource();  (int = 0; < 20; i++)     task.factory.startnew(() => { loadurl(i, cancelallsource.token); });  cancellationtokensource cancelimageloadssource = new cancellationtokensource();  // connect cancel token cancel image loads token cancelallsource.token.register(cancelimageloadssource.cancel, false);  (int = 0; < 10; i++)     task.factory.startnew(() => { loadimage(i, cancelimageloadssource.token); });  ...  cancelimageloadssource.cancel(); // cancel loadimage tasks // or cancelallsource.cancel(); // cancel tasks (including loadimage tasks) 

yes, there is.

you can use createlinkedtokensource* created cancellationtokensource can cancel yourself, automatically canceled when token signaled:

var cancelallsource = new cancellationtokensource(); var cancelimageloadssource = cancellationtokensource.createlinkedtokensource(cancelallsource.token); 

creates cancellationtokensource in canceled state when of source tokens in specified array in canceled state.

from cancellationtokensource.createlinkedtokensource


* if @ implementation isn't same, it's pretty close using cancellationtoken.register doing


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -