取消异步操作

发布于 2024-12-19 03:21:55 字数 948 浏览 2 评论 0原文

我正在开发一个实现协议栈的类库。我有一个实现 IAsyncResult 的异步操作 DoSomething。此方法的用户可以使用 BeginDoSomething 和 EndDoSomething。长期以来,这是一项很常见的任务。

现在我得到了使异步操作可中止的要求。我可以想到多种方法来实现此目的,但我不确定哪一种是最好的:

  1. 除了 BeginDoSomething 和 EndDoSomething 之外,还有一个 CancelDoSomething 方法。这不是常见的 asaync 模式,因此操作用户不容易发现。
  2. 根据没有。 1:具有 Cancel 属性
  3. BeginDoSomething 返回的 IAsyncResult 实例可以强制转换为 DoSomethingAsyncResult。 DoSomethingAsyncResult 有一个 Cancel 方法。同样,这不是异步模式的常见用法,并且对于类库的“普通用户”来说很难知道。
  4. 根据没有。 3:具有 Cancel 属性
  5. BeginDoSomething 返回的 IAsyncResult 实例实现 IAbortableAsyncResult: public IAbortableAsyncResult:IAsyncResult { void Abort(); } }。对我来说,这似乎是最优雅的方式。当用户使用 BeginDoSomething 开始操作时,他会获得一个 IAbortableAsyncResult 实例,这意味着向他显着介绍了这个特殊功能。

最好的解决方案是什么?还有更多的方法来解决这个问题吗?我宁愿不。 5,因为它没有代码味道(至少我到现在还没意识到)。

[编辑]

不依赖 TPL/.NET 4 的方法值得赞赏,因为它具有也适用于无法移植到最新版本的 .NET 框架的遗留代码的解决方案。

[/编辑]

I'm developing a class library that implements a protocol stack. There I have an asynchronous operation DoSomething that implements IAsyncResult. The user of this method can use BeginDoSomething and EndDoSomething. So long it's quite a common task.

Now I got the requirement to make the asynchronous operation abortable. I can think of several ways to achieve this, but I'm not sure which one is the best:

  1. Have a CancelDoSomething method besides BeginDoSomething and EndDoSomething. This is not the common asaync pattern and thus not easy to find out for the user of the operation.
  2. According to no. 1: Have a Cancel property
  3. The instance of IAsyncResult that is returned by BeginDoSomething can be casted to DoSomethingAsyncResult. DoSomethingAsyncResult has a Cancel method. Again, this is not the common usage of the async pattern and hardly to know for the "common user" of the class library.
  4. According to no. 3: Have a Cancel property
  5. The instance of IAsyncResult that is returned by BeginDoSomething implements IAbortableAsyncResult: public IAbortableAsyncResult:IAsyncResult { void Abort(); }. For me this seems to be the most elegant way. When the user starts the operation using BeginDoSomething he gets an instance of IAbortableAsyncResult which means that this special feature is prominently introduced to him.

What is the best solution? Are there even more ways to solve this? I prefer no. 5 because it doesn't have a code smell ( at least I didn't recognize until now).

[EDIT]

An approach not relying on TPL/.NET 4 is appreciated to have a solution that is also applicable in legacy code that cannot be ported to the newest version of .NET framework.

[/EDIT]

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

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

发布评论

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

评论(1

Hello爱情风 2024-12-26 03:21:55

CancellationToken 和 CancellationTokenSource 在 4.0 中被添加到 .net,并从此形成了通用取消模式的基础。通常,异步方法有一个需要 CancellationToken 的重载。

CancellationToken 的使用者可以在令牌被取消时注册回调,并使用回调来停止执行。或者,如果任务执行时间不是特别长,则可以间歇性地轮询 IsCanceled 属性。

CancellationToken是异步任务使用的接口,任务的发起者使用CancellationTokenSource来发出这个令牌,并且可以在将来取消它。

除了成为处理取消的标准方法之外,这种方法的好处之一是它是可组合的。如果该任务包含多个其他可取消的异步操作,则可以将相同的取消标记传递给它们,并且取消应该自动完成。

CancellationToken and CancellationTokenSource were added to .net in 4.0, and form the basis for a common cancellation pattern ever since. Generally, asynchronous methods have an overload which takes a CancellationToken.

Consumers of CancellationTokens may register for a callback when the token is canceled, and use the callback to stop executing. Alternatively, if the task is not particularly long-executing, the IsCanceled property can just be polled intermittently.

The CancellationToken is the interface used by the asynchronous task, the initiator of the task uses a CancellationTokenSource to issue this token, and may then cancel it in the future.

One of the nice things about this approach, other than it becoming a standard way to approach cancellation, is that it is composable. If the task composes several other cancellable asynchronous operations, the same cancellation token can be passed through to them and the cancellation should automatically work itself out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文