如何取消异步调用?在.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):
更多推荐
如何取消异步调用?
发布评论