c# - Communcate with object inside a Task -
i want know how or if required in case communicate object running inside task.
i have collection of processes, generic objects perform long running monitoring , calculating:
private ienumerable<iservice> _services; since based on common interface implement method "dosomework". arguments sakes lets call method dowork.
i want these methods run in separate task space , not block sequentially, spinning task list @ similar scope level run part of program.
private list<task> processtask = new list<task>(); private cancellationtokensource tokensource = new cancellationtokensource(); private cancellationtoken token; private void startall() { token = tokensource.token; processtask = _services.select(service => task.factory.startnew( () => startservice(service), token, taskcreationoptions.longrunning, taskscheduler.current)).tolist(); } the startservice method starts monitoring , work on individual item:
private void startservice(iservice plugin) { ... ... plugin.dowork(); ... ... } the services have method "stop" , "continue", leads question. in heart of using tasks best trying find way of influencing service withing task using event or delegate , pausing/stopping task or calling these methods on item external _services collection?
e.g.:
_services.foreach(item => item.stop()); if former how raise event inside task out side, or should monitor external flag?
many in advance.
you can use cancellationtoken one-shot event:
token.register(() => item.stop()) hopefully, work particular "service" api. note, token can fire before service starts , after done.
Comments
Post a Comment