AngouriMath

Navigation

← Back to list of members

SetLocalCancellationToken​(System.​Threading.​CancellationToken)

 Method (no overloads)

Summary

Sets the thread-local cancellation token

Parameter "token"

Example

Entity eq = "a e sin(x ^ 14 + 3)3 + a b c d sin(x ^ 14 + 2)4 - k d sin(x ^ 14 + 3)2 + sin(x ^ 14 + 3) + e = 0";

using var tokenSource = new CancellationTokenSource();
tokenSource.CancelAfter(millisecondsDelay: 1000);
Multithreading.SetLocalCancellationToken(tokenSource.Token);

var task = Task.Run(() => eq.Solve("x"));

try
{
    while (!task.IsCompleted)
    {
        Thread.Sleep(100);
        Console.WriteLine("Not completed yet. Waiting 100 ms...");    
    }
    Console.WriteLine(task.Result);
}
catch (AggregateException e)
{
    if (e.InnerExceptions.AsEnumerable().Any(c => c is OperationCanceledException))
        Console.WriteLine("Operation cancelled");
    else
        throw;
}

Prints
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Not completed yet. Waiting 100 ms...
Operation cancelled

























Angouri © 2019-2023 · Project's repo · Site's repo · Octicons · Transparency · 1534 pages online