开发者

ReactiveUI messagebus

I'm testing ReactiveUI, it seems very nice.

However, I am a bit puzzled by the MessageBus.

Sample code :

var bus = new MessageBus();
int result = -1;

bus.Listen<int>().Subscribe(x => result = x);
bus.SendMessage(42);

It does work when calling an Assert statement, but in a standard WPF application the result value is never updated. This is probably due to the Sche开发者_运维技巧duler implementation, but it's not quite clear to me yet.

Any hint is welcome.


The result is eventually updated (the same as calling Dispatcher.BeginInvoke), not immediately. By default, RxUI schedules things differently in a unit test runner to make it easier to write unit tests - that's why you see that warning in the unit test runner output.

If you were to instead do something like:

var bus = new MessageBus();

bus.Listen<int>().Subscribe(x => MessageBox.Show("The answer is " + x));
bus.SendMessage(42);

You will see the Message Box (if not, it's definitely a bug!).

Why is the MessageBus deferred? It makes it easier to write async code, since you can now SendMessage from other threads without seeing WPF's dreaded InvalidOperationException due to accessing objects on the wrong thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜