How to cancel an asynchronous call? 异步调用 的中断 取消 c#
来源:互联网 发布:编程入门书籍 编辑:程序博客网 时间:2024/05/18 02:55
A "cancel flag" is the way to do it, though not a global one, necessarily. The unavoidable point is that you needsome way to signal to the thread that it should stop what it's doing.
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):
- Use the
BackgroundWorker
instead ofBeginInvoke
. This hascancellation 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 aCancel()
method that sets aCancelled
flag that the executing code can check. Of course you'll need to keep a reference to the state object so you can callCancel()
on it (which is something theBackgroundWorker
does for you - you have a component on your form. (Thanks toFredrik for reminding about this). - Create your own
ThreadStart
delegate, passing in a state object as with option 2.
详见:http://stackoverflow.com/questions/1729346/how-to-cancel-an-asynchronous-call
- How to cancel an asynchronous call? 异步调用 的中断 取消 c#
- How to cancel an invoice
- 如何异步调用Visual C#函数How to call a Visual C# method asynchronously
- 异步过程调用(Asynchronous Procedure Call)
- When An SO is closed ,how to cancel it
- How to run an ajax call after another ajax call?
- How to Call C# DLL in Delphi7
- 注入(2)--APC(Asynchronous Procedure Call)注入(异步过程调用)
- How to call an external C function from within Oracle
- How to call different methods as given in an array
- C# 代理的异步特性(Asynchronous Nature of Delegates)
- how to cancel autoplay in xp
- html:cancel按钮的作用,cancel按钮,取消按钮
- An Introduction to Asynchronous Programming and Twisted
- How to terminate call
- How to obtain asynchronous response synchronously?
- How to call c++ exported method and classes in c#
- C#的异步调用
- c++中的单例模式
- MySql与Java的时间类型
- SVN记录转excel文件的小程序(windows版本)
- ios学习笔记之UI篇(五):切换视图
- JS实现局部打印和预览
- How to cancel an asynchronous call? 异步调用 的中断 取消 c#
- GTK+2.0 中的容器控件与布局技巧
- UAP Resource
- C语言数组下标太大怎么办?
- 快速排序比较好的一种理解方式
- ios:用代码向视图中添加各种控件
- Pellet对SWRL的支持
- POJ_2485 Highway_Prim
- 身体锻炼,饮食健康,生活规律,心情愉悦