如何取消CSharpScript.RunAsync [英] How to cancel CSharpScript.RunAsync

查看:297
本文介绍了如何取消CSharpScript.RunAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何停止RunAsync?

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()
}

我还能怎么做。以及为什么这些方法需要cancelToken参数?

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

推荐答案

您必须在等待调用,它将阻止执行直到获得结果为止,例如:

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;

这篇关于如何取消CSharpScript.RunAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆