How to cancel CSharpScript.RunAsync

坚强是说给别人听的谎言 提交于 2019-12-12 12:32:47

问题


How can I stop RunAsync?

CancellatioTokenSource cts = new CancellationTokenSource();
//I thought that it's must work, but it don't
var script = CSharpScript.Create(code: someCode);
await script.RunAsync(cancelletionToken: cts.Token);

void button_click()
{
   cts.Cancel()
}

How else can I do this. And for why such methods need cancellationToken parameter?


回答1:


You must do it before the await call, which blocks execution until yout get results, e.g.:

var task = script.RunAsync(cancelletionToken: cts.Token);

cts.Cancel();

var result = await task;


来源:https://stackoverflow.com/questions/41223259/how-to-cancel-csharpscript-runasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!