The basic implementation of the observer OnError
(as well as other functions) contains:
if (Interlocked.Exchange(ref this.isStopped, 1) == 0)
{
this.OnErrorCore(error);
}
The value is isStopped
set to “stopped” when the token is canceled. The observer takes care of the cancellation process and does not need to be manually controlled.
You can easily check it if you change the code OnNext
to
if(str == "B")
token.Cancel();
The result will be:

Even if you delete the statement if
. After you cancel the token, the functions will not inherit
var observable = Observable.Create<string>(
async (o, c) =>
{
var strings = new[] { "A", "B", "C" };
foreach (var s in strings)
{
await Task.Delay(100);
o.OnNext(s);
}
o.OnCompleted();
});
, ( ), - , :
if (c.IsCancellationRequested)
{
// exception thrown here.
Console.WriteLine("cancelled");
tcs.SetResult(true); // instead of throwing exceptions
// some other clean up code or/and return statement
}