c# - Several tasks cancellation -


there 1 top task launches task.

var token = _cancellationtokensource.token; _task = new task(() => workaction(this, token), token); //top level task 

the inner task implementation is:

public string finddevice(iprogressviewmodel progressviewmodel,                           cancellationtoken cancellationtoken) {          (int = 0; < orderedcomportslist.count; i++) {             try {                 cancellationtoken.throwifcancellationrequested();             } catch (operationcanceledexception) {                 return null;             }             string curport = orderedcomportslist[i];             if (innerisdeviceonline(devicemodelid, curport)) //the real long work                 return curport;             progressviewmodel.currentvalue = i;         }         return null;     } 

with implementation cancellation occur when last innerisdeviceonline ended. how implement finddevice make cancellable immediately?

update 1.

can that:

 bool? result = null;             task<bool> parent = new task<bool>(() => {                 bool? res = result;                 task.factory.startnew(() => {                     while (res==null) {                                                    cancellationtoken.throwifcancellationrequested();                         thread.sleep(50);                     }                 }, taskcreationoptions.attachedtoparent);                  result = innerisdeviceonline(devicemodelid, comport);                 return result.value;             }, cancellationtoken);              return parent; 

the problem never finishes (i don't know why). give immediate cancellation.

in order gracefully cancel long-running operations, entire api needs support cancellation @ specific granularity need. in cases api long-running operation not support cancellationtoken, you'll need provide interfacing layer provide support. example, asynchronous webrequest.begingetresponse not support cancellationtoken, able support through extension method, calls abort() in response cancellationtoken getting canceled:

https://github.com/openstacknetsdk/openstack.net/blob/master/src/corelib/core/webrequestextensions.cs


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -