如何观察调用线程 [英] How to observe on calling thread

查看:52
本文介绍了如何观察调用线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用了很多阻止操作

I have a number of blocking operations that I start using

Observable.Start(()=>阻塞的东西)

Observable.Start( () => Something that blocks )

然后.Merge返回的observables

I then .Merge the observables returned

当操作返回时我希望能够开始处理它们但是我想在调用线程上进行处理(在这种情况下是ASP) .Net worker thread。。

As the operations return I want to be able to begin processing them but I want to do the processing on the calling thread (in this case an ASP.Net worker thread).

看起来我应该可以在Merge之后调用ObserveOn,但我不知道如何在调用线程上进行处理。

It looks like I should be able to call ObserveOn after Merge but I've got no idea how to get the processing to happen on the calling thread.

我已经尝试了同步会话。但是它总是为空。

I've tried picking up SynchronizationContext.Current but it is always null.

我缺少什么?

推荐答案

多年来我没看过(真正的)asp.net开发,所以我可能错了,但我不认为ASP.NET工作线程正在消息循环中运行。所以,如果不是这样,那么当它喜欢的时候Rx怎么能够在这个线程上出现并开始运行代码

I have not done a look of (real) asp.net development in years so I may be wrong, but I dont think that the ASP.NET worker thread is running in a message loop. So if it is not, then how will Rx be able to just come along and start running code back on this thread when it likes?

所以想象你的代码是需要的运行方法A(),B(),C()和D()。想象一下,说B()做你的Observable.Start东西并开始工作到另一个线程。完成后,需要运行回调(通过.Subscribe(...)运算符)。

So imagine your code is needs to run methods A(), B(), C() and D(). Imagine that say B() does your Observable.Start stuff and kicks off work onto another thread. When it is done a callback needs to be run (via your .Subscribe(...) operator).

A();
B(); //Calls Observable.Start somewhere in here.
C();
D();

如果您希望代码在此主线程上运行,代码将在何处运行?编译器是否会决定它应该停止而不是处理C()或D()并改为回拨?在WPF和其他具有消息循环的事物中,我们将操作作为消息
放在队列(在WPF的情况下为Dispatcher)中,并且不断处理。这意味着回调只是放入此队列的消息。

If you want the code to run on this main thread, where will the code run? Would the compiler just decide that it should stop and not process C() or D() and do your call back instead? In WPF and other things that have message loops, we put actions as message on a queue (the Dispatcher in the case of WPF) that is constantly processed. This means that callbacks are just messages put on to the this queue.

您可能想要做的事情是调用Ob.Start()事物然后将结果放入队列中。代码完成后,它可以排空队列吗?公平地说,当我说这样的事情时,我的头脑中听起来有点不对劲,我认为TPL可能是
,这是一个更好的工具。您使用Rx的原因是什么?

Potentially what you want to do it call your Ob.Start() things and then put the results into a queue. Once your code has finished it can drain the queue? To be fair, when I say things like this it just sounds wrong in my head and I think TPL is probably a far better tool for the job. Any reason why you are using Rx?

Lee


这篇关于如何观察调用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆