If a CancellationToken is a structure and passed in a value, how is it updated?

I see that CancellationToken is a structure https://docs.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken?view=netframework-4.7.1

If I pass the structure of a new function by value, it should not be changed in the caller. Therefore, if I transfer the CancellationTokenSource file (by value), then I call cts.cancel (), how is a method that has a copy of this token notified that it has been canceled? Doesn't that work only if we follow the link?

For instance:

public static void Main()
{
    var cts = new CancellationTokenSource();
    SomeCancellableOperation(cts.Token);
    cts.cancel();
}

public void SomeCancellableOperation(CancellationToken token) {
...
    token.ThrowIfCancellationRequested();
...
}
+4
source share
2 answers

CancellationToken. , CancellationTokenSource.

internal CancellationToken(CancellationTokenSource source)
{
    m_source = source;
}

The CancellationTokenSource - , , . , , :

public bool IsCancellationRequested 
{
    get
    {
        return m_source != null && m_source.IsCancellationRequested;
    }
}
+5

, WaitHandle. WaitHandle .

+1

Source: https://habr.com/ru/post/1691841/