BehaviorSubject和CombineLatest - 奇怪的行为

时间:2012-06-13 09:13:52

标签: c# system.reactive

在以下代码中:

public class Connection
{
    private BehaviorSubject<string> addressSubject = new BehaviorSubject<string>("null");
    private EventLoopScheduler scheduler = new EventLoopScheduler(ts => new Thread(ts));
    private IDisposable reconnectSubscription = null;

    public Connection()
    {
        this.reconnectSubscription = this.addressSubject
            .ObserveOn(this.scheduler)
            .CombineLatest(Observable.Interval(TimeSpan.FromSeconds(1), this.scheduler), (x, y) => x)
            .Subscribe(x => Debug.WriteLine(x));
    }    

    public void Connect(string address)
    {
        Observable.Start(() => this.addressSubject.OnNext(address), this.scheduler);
    }

    public void Disconnect()
    {
        Observable.Start(() => this.addressSubject.OnNext(null), this.scheduler);
    }
}

调用'Connect'后会打印。

null
localhost
null
localhost
null
localhost

我认为这是一些线程问题,因为如果我在构造函数中调用Connect它会按预期工作。

有什么建议吗?

0 个答案:

没有答案