4

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?

Sinatr
  • 20,892
  • 15
  • 90
  • 319
itihonov
  • 81
  • 1
  • 7

1 Answers1

3

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;
m0sa
  • 10,712
  • 4
  • 44
  • 91