如何取消异步调用?

编程入门 行业动态 更新时间:2024-10-25 22:36:18
本文介绍了如何取消异步调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何取消异步调用?在.NET APM似乎并不支持此操作。

How to cancel an asynchronous call? The .NET APM doesn't seem to support this operation.

我有以下的环在我的code的派生多个线程的线程池。当我点击我的用户界面的一个按钮,我想这些线程(或异步调用)结束。

I have the following loop in my code which spawns multiple threads on the ThreadPool. When I click a button on my UI, I would like these threads (or asynchronous calls) to end.

foreach (var sku in skus) { loadSku.BeginInvoke(... }

有没有优雅的解决方案不是创建一个全球性的取消标记,并具有异步方法来寻找它的其他?

Is there any elegant solution other than creating a global "Cancel flag" and having the asynchronous methods to look for it?

推荐答案

取消标记,就是要做到这一点,虽然不是全球性的,不一定。不可避免的一点是,你需要的一些的方式发信号给线程,它应该停止它在做什么。

A "cancel flag" is the way to do it, though not a global one, necessarily. The unavoidable point is that you need some way to signal to the thread that it should stop what it's doing.

在的BeginInvoke 的情况下,这是很难做什么,但一个全球性的标志,因为工作开展的线程池,而你不知道哪个线程。你有几个选项(在顺序preference):

In the case of BeginInvoke, this is hard to do with anything but a global flag, because the work is carried out on the threadpool, and you don't know which thread. You have a couple of options (in order of preference):

  • 使用的BackgroundWorker ,而不是的BeginInvoke 的。这有cancellation功能烤入。这有其他好处,如progress监测和"Work完整的回调,也很好处理异常的。
  • 使用 ThreadPool.QueueUserWorkItem ,传递一个对象具有状态的取消()方法,设置一个取消标志,正在执行的code可以检查。当然,你需要保持一个参考状态对象,所以你可以调用取消()就可以了(这是后话了的BackgroundWorker 为你做 - 你有一个组件的形式(感谢弗雷德里克提醒一下。这一点)。
  • 创建你自己的的ThreadStart 委托,传递一个状态对象与选项2。
  • Use the BackgroundWorker instead of BeginInvoke. This has cancellation functionality baked-in. This has other benefits, like progress monitoring, and "Work complete" callbacks. It also nicely handles exceptions.
  • Use ThreadPool.QueueUserWorkItem, passing in an object as the state that has a Cancel() method that sets a Cancelled flag that the executing code can check. Of course you'll need to keep a reference to the state object so you can call Cancel() on it (which is something the BackgroundWorker does for you - you have a component on your form. (Thanks to Fredrik for reminding about this).
  • Create your own ThreadStart delegate, passing in a state object as with option 2.
  • 更多推荐

    如何取消异步调用?

    本文发布于:2023-11-12 19:00:02,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1582279.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!