gpt4 book ai didi

c# - 如何取消 CSharpScript.RunAsync

转载 作者:行者123 更新时间:2023-11-30 16:45:56 29 4
gpt4 key购买 nike

如何停止 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()
}

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

最佳答案

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

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

cts.Cancel();

var result = await task;

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

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com