如何停止 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 参数?

最佳答案

您必须在 await 调用之前执行此操作,这会阻止执行直到您获得结果,例如:

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

cts.Cancel();

var result = await task;

关于c# - 如何取消 CSharpScript.RunAsync,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41223259/

10-12 19:42