Observable.Create:CancellationToken没有转换到IsCancellationRequested

时间:2016-11-09 11:25:29

标签: c# .net system.reactive

采用这个小脚本(在LINQPad中设计,但应该在任何地方运行):

void Main()
{
    Task.Run(() => Worker()).Wait();
}

async Task Worker()
{
    if (SynchronizationContext.Current != null)
        throw new InvalidOperationException("Don't want any synchronization!");

    BaseClass provider = new Implementation();
    Func<IObserver<TimeSpan>, CancellationToken, Task> subscribeAsync =
        provider.CreateValues;
    var observable = Observable.Create(subscribeAsync);

    var cancellation = new CancellationTokenSource(5500).Token; // gets cancelled after 5.5s
    cancellation.Register(() => Console.WriteLine("token is cancelled now"));
    await observable
        .Do(ts =>
        {
            Console.WriteLine("Elapsed: {0}; cancelled: {1}",
                ts,
                cancellation.IsCancellationRequested);
            cancellation.ThrowIfCancellationRequested();
        })
        .ToTask(cancellation)
        .ConfigureAwait(false);
}

abstract class BaseClass
{
    // allow implementers to use async-await
    public abstract Task CreateValues(IObserver<TimeSpan> observer, CancellationToken cancellation);
}

class Implementation : BaseClass
{
    // creates Values for 10s; entirely CPU-bound: no way for async-await hence return Task.CompletedTask
    public override Task CreateValues(IObserver<TimeSpan> observer, CancellationToken cancellation)
    {
        try
        {
            var sw = Stopwatch.StartNew();
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine("{0}/{1} cancelled:{2}", i, j, cancellation.IsCancellationRequested);
                    Thread.Sleep(333);
                }

                if (cancellation.IsCancellationRequested) // !! never gets true !!
                    throw new ApplicationException("token is cancelled");

                observer.OnNext(sw.Elapsed);
            }

            return Task.CompletedTask;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            throw;
        }
    }
}

方法Implementation.CreateValues justs在整个10秒内保持运行,而不是在5.5秒后停止。 CancellationToken传入的Observable.Create甚至无法转换为已取消状态(当然原始令牌也是如此)!

这是一个错误吗?做错事是我的错吗?

输出是:

0/0 cancelled:False
0/1 cancelled:False
0/2 cancelled:False
Elapsed: 00:00:01.0205951; cancelled: False
1/0 cancelled:False
1/1 cancelled:False
1/2 cancelled:False
Elapsed: 00:00:02.0253279; cancelled: False
2/0 cancelled:False
2/1 cancelled:False
2/2 cancelled:False
Elapsed: 00:00:03.0274035; cancelled: False
3/0 cancelled:False
3/1 cancelled:False
3/2 cancelled:False
Elapsed: 00:00:04.0294796; cancelled: False
4/0 cancelled:False
4/1 cancelled:False
4/2 cancelled:False
Elapsed: 00:00:05.0315332; cancelled: False
5/0 cancelled:False
5/1 cancelled:False
token is cancelled now
5/2 cancelled:False
Elapsed: 00:00:06.0335601; cancelled: True
6/0 cancelled:False
6/1 cancelled:False
6/2 cancelled:False
Elapsed: 00:00:07.0436211; cancelled: True
7/0 cancelled:False
7/1 cancelled:False
7/2 cancelled:False
Elapsed: 00:00:08.0457921; cancelled: True
8/0 cancelled:False
8/1 cancelled:False
8/2 cancelled:False
Elapsed: 00:00:09.0477509; cancelled: True
9/0 cancelled:False
9/1 cancelled:False
9/2 cancelled:False
Elapsed: 00:00:10.0498751; cancelled: True
[AggregateException] at Main/Task.Wait()

2 个答案:

答案 0 :(得分:4)

传递给subscribeAsync函数的取消令牌由Observable.Create调用实例化,而不是您要实例化的取消令牌。

根据Observable.Create重载摘要:

  

从指定的可取消创建可观察序列   异步订阅方法。 CancellationToken传递给了   异步订阅方法与返回的一次性绑定   订阅,允许尽力取消。

简而言之,取消订阅将在您处置订阅时取消,而不是在指定的延迟之后取消。

您应该能够按如下方式重构代码以使其正常工作:

Observable.Create(observer => subscribeAsync(observer, cancellation));

希望它有所帮助。

答案 1 :(得分:0)

这不是问题的答案,而是使用System.Threading.Tasks.Dataflow代替System.Reactive重写示例代码(过多的代码作为评论发布):

这有几个好处:

  1. 由于observer参数现在为Task,因此每个实现都有await的内容。
  2. 以前在Do()(现在在ActionBlock中)的处理代码本身可以根据需要实现为异步。
  3. 如果需要,可以进行集成缓冲。
  4. 它的解耦=技术不可知:我的界面是Func<TimeSpan, Task<bool>>,因此不依赖于Rx或TPL-Dataflow或其他什么。
  5. 新代码:

    void Main()
    {
        Task.Run(() => Worker()).Wait();
        Console.WriteLine("DONE");
    }
    
    async Task Worker()
    {
        if (SynchronizationContext.Current != null)
            throw new InvalidOperationException("Don't want any synchronization!");
    
        var cancellation = new CancellationTokenSource(55000).Token; // gets cancelled after 5.5s
        cancellation.Register(() => Console.WriteLine("token is cancelled now"));
    
        var flow = new ActionBlock<TimeSpan>(
            async ts =>
            {
                Console.WriteLine("[START] Elapsed: {0}; cancelled: {1}", ts, cancellation.IsCancellationRequested);
                await Task.Delay(2500).ConfigureAwait(false); // processing takes more time than items need to produce
                Console.WriteLine("[STOP] Elapsed: {0}; cancelled: {1}", ts, cancellation.IsCancellationRequested);
            },
            new ExecutionDataflowBlockOptions
            {
                BoundedCapacity = 2, // Buffer 1 item ahead
                EnsureOrdered = true,
                CancellationToken = cancellation,
            });
    
        Func<TimeSpan, Task<bool>> observer = ts => flow.SendAsync(ts, cancellation);
    
        BaseClass provider = new Implementation();
        await provider.CreateValues(observer, cancellation).ConfigureAwait(false);
        Console.WriteLine("provider.CreateValues done");
    
        flow.Complete();
        await flow.Completion.ConfigureAwait(false);
        Console.WriteLine("flow completed");
    }
    
    abstract class BaseClass
    {
        // allow implementers to use async-await
        public abstract Task CreateValues(Func<TimeSpan, Task<bool>> observer, CancellationToken cancellation);
    }
    
    class Implementation : BaseClass
    {
        public override async Task CreateValues(Func<TimeSpan, Task<bool>> observer, CancellationToken cancellation)
        {
            try
            {
                var sw = Stopwatch.StartNew();
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.WriteLine("{0}/{1} cancelled:{2}", i, j, cancellation.IsCancellationRequested);
                        Thread.Sleep(333);
                    }
    
                    if (cancellation.IsCancellationRequested)
                        throw new ApplicationException("token is cancelled");
    
                    var value = sw.Elapsed;
                    var queued = await observer(value); // use of "observer" encorces async-await even if there is nothing else async
                    Console.WriteLine("[{0}] '{1}' @ {2}", queued ? "enqueued" : "skipped", value, sw.Elapsed);
    
                    if (!queued)
                        ; // Dispose item
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
    }