如何取消使用 Rx 调用的异步方法

发布于 2024-11-10 16:51:02 字数 360 浏览 0 评论 0原文

我想知道如何使用 TakeUntil 取消\停止使用反应式扩展调用的异步方法。

所以我希望能够取消\停止以下方法:

this.booksService.Search(searchText)
                 .ObserveOnDispatcher()
                 .Subscribe(results =>
                 {
                    this.books.Clear();
                    this.books.AddRange(results);
                 });

I want to know how to cancel\stop asynchronous method called using reactive extensions using the TakeUntil.

So I want to be able to cancel\stop the following method:

this.booksService.Search(searchText)
                 .ObserveOnDispatcher()
                 .Subscribe(results =>
                 {
                    this.books.Clear();
                    this.books.AddRange(results);
                 });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-11-17 16:51:02

当您处置该值时,Subscribe() 返回的是一个 IDisposable,如下所示:

var searchSub = this.booksService
                    .Search(searchText) ...
                    .Subscribe( ... );
// do something else
searchSub.Dispose();

订阅停止。这真的会阻止底层的图书服务搜索吗?这取决于实施情况。如果使用 Observable.Create 完成,那么很可能会出现这种行为。

The return from Subscribe() is an IDisposable, when you dispose the value, like so:

var searchSub = this.booksService
                    .Search(searchText) ...
                    .Subscribe( ... );
// do something else
searchSub.Dispose();

the subscription is stopped. Does that actually stop the underlying booksSevice search? That depends on the implementation. If done w/ Observable.Create then it's quite possible to get that behavior.

嗼ふ静 2024-11-17 16:51:02

您必须告诉 booksService 取消搜索。 Search 方法启动搜索并返回一个 IObservable。搜索完成后,将在 IObservable 上调用 OnNextSearch 中启动了一些异步代码,您需要取消它们。

You will have to tell booksService to cancel the search. The Search method starts the search and also returns an IObservable. When the search is complete OnNext will be called on the IObservable. There is some asynchronous code started in Search that you need to cancel.

独行侠 2024-11-17 16:51:02

我的建议是将搜索方法更改为异步调用,该调用将 CancellationToken 作为第二个参数。然后,您可以通过 Observable.ToAsync 将其转换为 Observable 序列。对此可观察量调用 dispose 将触发 CancellationToken 的取消。这样做可以让您轻松地将令牌传递到异步 Web 调用等。

public Task Search(string searchText, CancellationToken cancel)
{
    // Pass the token on to the async webclient request etc.
}

var searchSub = Observable.ToAsync(c => this.booksService.Search(searchText, c))
                .Subscribe( ... );
// do something else
searchSub.Dispose();

My recommendation is to change the search method to an asynchronous call that takes a CancellationToken as the second argument. You can then convert this to an Observable sequence via Observable.ToAsync. Calling dispose on this observable will then trigger the cancellation of the CancellationToken. Doing this allows you to easily pass the token onto asynchronous web calls etc.

public Task Search(string searchText, CancellationToken cancel)
{
    // Pass the token on to the async webclient request etc.
}

var searchSub = Observable.ToAsync(c => this.booksService.Search(searchText, c))
                .Subscribe( ... );
// do something else
searchSub.Dispose();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文