如何使用UniRx Observable.Timeout设置超时时间?

劳伦斯·陈

我尝试使用observable.Timeoutset timeout,但是结果超出了我的预期。
像这样的代码:

public class ObservableTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        var subj = new Subject<int> ();
        new System.Threading.Thread (() => {
            Debug.Log("thread id - " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            var i = 0;
            while(i < 10) {
                i += 1;
                System.Threading.Thread.Sleep(i*1000 );
                subj.OnNext(i);
            }
        }).Start();


        var timeout = subj.Timeout (System.TimeSpan.FromSeconds (4));
        timeout.Subscribe (x => Debug.Log("x - " + x), ex => Debug.LogError(ex), () => Debug.Log("completed"));
    }

}

控制台输出:

x - 1
x - 2
...
x - 10

为什么不先中断x - 5?如何将超时设置为可观察的时间?

更新

有时输出中断,x - 8我不知道它是如何工作的。

恩托尔

我已经检查了问题。本文的二手Spy()扩展>如何查看我的反应式扩展查询在做什么?

问题可能是调用者的线程处理不正确。这个对我有用。x-2之后断开连接。

var subj = new Subject<int>();
new Thread(() => {
    Console.WriteLine("thread id - " + Thread.CurrentThread.ManagedThreadId);
    var i = 0;
    while (i < 4)
    {
        i += 1;
        Thread.Sleep(i * 1000);
        subj.OnNext(i);
    }
    subj.OnCompleted();
}).Start();

//var timeout = subj.Timeout(TimeSpan.FromSeconds(0.5));
var timeout = subj.Timeout(TimeSpan.FromSeconds(2));
IDisposable disp = timeout.Spy().Subscribe(x => Debug.WriteLine("x - " + x), ex => Debug.WriteLine(ex.Message), () => Debug.WriteLine("completed"));
// That might be missing.
Thread.CurrentThread.Join(10500);
disp.Dispose();

输出

Debug Trace:
IObservable: Observable obtained on Thread: 14, 03:05:24.28
IObservable: Subscribed to on Thread: 14, 03:05:24.289
IObservable: Subscription completed. 03:05:24.296
IObservable: OnNext(1) on Thread: 17, 03:05:25.258 //<- i = 1. One sec wait.
x - 1
IObservable: OnNext(2) on Thread: 17, 03:05:27.261 //<- i = 2. Two sec wait. It's correct.
x - 2
IObservable: OnError(System.TimeoutException: The operation has timed out.) on Thread: 13, 03:05:29.264 //<- i = 3. Two sec timeout comes into play. Correct. Next step would need 3 sec to finish.
The operation has timed out.
IObservable: Cleaned up on Thread: 13, 03:05:29.266

我有不同的输出,这也是正确的

Debug Trace:
IObservable: Observable obtained on Thread: 14, 03:20:17.714
IObservable: Subscribed to on Thread: 14, 03:20:17.723
IObservable: Subscription completed. 03:20:17.73
IObservable: OnNext(1) on Thread: 17, 03:20:18.693 //<- 1 sec passed since subscription. correct
x - 1
IObservable: OnError(System.TimeoutException: The operation has timed out.) on Thread: 12, 03:20:20.698 //<- 2 sec passed, and that was the timeout. There was a race between timeout and OnNext(2). Timeout win. Correct.
The operation has timed out.
IObservable: Cleaned up on Thread: 12, 03:20:20.7

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

JJWT代币。如何设置超时时间?

如何设置SQLQuery的超时时间?

如何设置TcpClient的超时时间?

如何设置弹出窗口的超时时间?

如何使用Angular设置HttpHandler的超时时间?

如何使用Intern设置功能测试的超时时间?

如何在Spring Cloud Gateway中设置超时时间?

Android:AndroidHttpClient-如何设置超时时间?

如何在课堂上设置更高的超时时间

如何设置sudo要求输入密码的超时时间?

如何设置SqlTransaction.Save()的超时时间?

如何为REST保证的请求设置超时时间?

如何在RDS Postgres中设置查询超时时间

如何设置Net :: HTTP.start的超时时间?

如何使Div变灰并设置超时时间

如何在Rust中使用hyper,tokio和Future设置HTTP请求的超时时间?

如何使用 Python 和 Windows 设置下载 YouTube 视频音频的超时时间

Python通过win32com运行MessageQueue.Peek,如何正确设置超时时间?

如何设置grub超时时间和grub默认启动项?

在golang中执行net.DialTCP时如何设置超时时间?

如何设置onFailure事件(春季,卡夫卡)的超时时间?

使用电池或交流电时设置不同的黑屏超时时间

如何逐步加快游戏的超时时间

如何增加“ yarn install”的超时时间

如何减少TcpOutboundGateway的超时时间?

设置超时时间

弹性beantalk部署花费的时间超过超时时间,如何增加超时时间

如何使用RxJS调整重试超时时间?

如何使用 Nautilus 延长 SFTP 连接的超时时间?