任何人都可以在以下场景中解释Take的处置行为 [英] Can anyone explain the dispose behaviour of Take in the following scenario

查看:109
本文介绍了任何人都可以在以下场景中解释Take的处置行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么我在使用Take(1)的流上看到订阅的不同处置行为,并且取决于我是否为订阅定义了OnCompleted操作?

Can anyone explain why I'm seeing different dispose behaviour for a subscription on a stream where Take(1) is used and depending on whether I define an OnCompleted action for the subscription?

基本上,当我定义一个OnCompleted动作时,我可以看到底层复合一次性是由Rx处理的,但是当它没有被定义时则不是。

Basically when I define an OnCompleted action I can see the under lying composite disposable is being disposed by Rx but when it is not defined then it isn't.

我写了一个博客文章关于Take(1) - 忽略第一部分2截图到最后显示我的意思...

I wrote a blog post about Take(1) - ignore the first part the 2 screenshots towards the end show what i mean...

http://awkwardcoder.blogspot.co.uk/2012/09/listening-to-single-value-from-rx。 html

平台:win8 CTP上的WTF

Platform: WTF on win8 CTP

Rx:NuGet的最新版本

Rx: latest version from NuGet

ta

Ollie

试着记住我学到的东西y

Trying to remember what I learned yesterday

推荐答案

您好Ollie,

Hi Ollie,

IsDisposed 是内部的属性,那么是什么让你认为 它反映了当你的observable完成时
点击事件的实际状态?

IsDisposed is an internal property, so what makes you think that it reflects the actual state of the Click event when your observable completes?

更好的测试将是创建自己的活动 并查看处理程序何时取消注册。 例如:

A better test would be to create your own event and see when the handler is unregistered.  For example:

using System;
using System.Reactive.Linq;

namespace RxLabs
{
	public static class EventLab
	{
		internal static void Main()
		{
			var foo = new Foo();

			var subscription = Observable.FromEventPattern(foo, "Event")
				.Take(1)
				.Subscribe(Console.WriteLine);

			foo.RaiseEvent();

			Console.ReadKey();
		}

		class Foo
		{
			private EventHandler @event;

			public event EventHandler Event
			{
				add
				{
					Console.WriteLine("Add");

					@event += value;
				}
				remove
				{
					Console.WriteLine("Remove");

					@event -= value;
				}
			}

			public void RaiseEvent()
			{
				@event(this, EventArgs.Empty);
			}
		}
	}
}

输出:

Add
System.Reactive.EventPattern`1[System.Object]
Remove

或者,你可以换行使用 Observable.Create 进行WPF查询以附加您自己的一次性用户。 然后你可以安全地检查它是否被处理掉。 例如:

Alternatively, you can wrap your WPF query with Observable.Create to attach your own disposable.  Then you can safely check whether it's disposed.  For example:

using System;
using System.Diagnostics;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows;

namespace TestRx.Wpf
{
	public partial class MainWindow : Window
	{
		private int count;
		private readonly BooleanDisposable subscription = new BooleanDisposable();

		public MainWindow()
		{
			InitializeComponent();

			var query = Observable.Create<EventPattern<RoutedEventArgs>>(
				observer =>
				{
					var s = Observable
						.FromEventPattern<RoutedEventArgs>(clickButton, "Click")
						.Take(1)
						.Subscribe(observer);

					return new CompositeDisposable(s, subscription);
				});

			query.Subscribe(_ => Debug.WriteLine("{0}: Click called...", ++count));
		}

		private void checkButton_Click(object sender, RoutedEventArgs e)
		{
			Debug.WriteLine("IsDisposed: {0}", subscription.IsDisposed);
		}
	}
}

输出:

(Check) IsDisposed: False
(Click) 1: Click called...
(Check) IsDisposed: True

我认为您可以通过这两种方法中的任何一种找到处理按预期进行处理,并且添加
onCompleted 回调无关紧要。

I think you'll find with either of these two approaches that disposal occurs as expected and that adding an onCompleted callback is irrelevant.

OT:注意您不必使用 .ObserveOn(Scheduler.CurrentThread)。 请参阅

此讨论
了解更多信息。

OT: Note that your use of .ObserveOn(Scheduler.CurrentThread) is unnecessary.  See this discussion for more info.

- 戴夫


这篇关于任何人都可以在以下场景中解释Take的处置行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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