0

在以下代码中:

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);
    }
}

调用“连接”后,它会打印。

null
localhost
null
localhost
null
localhost

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

有什么建议么?

4

0 回答 0