开发者

How to abort asynchronous method?

I get data from asynchronous method. E.g i have two ListBox .When selection changed event occuered in 1st listbox, it sends request to asynchronous method, and gets 2nd listbox's items source. If before this async method completed, change of selec开发者_StackOverflow社区tion in first ListBox, method raises exception.How to abort previus async call before it is completed?


this will need some work. Easiest way at the moment might be the Rx-Framework (Reactiveextensions). Have a look at this site

If you don't want to go this road (need some learning), than you have to provide some mechanism to stop running async. requests. You can do this with System.Threading.CancellationTokenSource or with a System.Threading.ManualResetEvent. Both can be used to flag a cancellation to a running task/thread, but the first might be clearer.

Most async. request can be called with a CancellationToken (just use the .Token-Property of the source) as far as I know. If not you can use your own Thread and check with ThrowIfCancellationRequested.

Or you can check on your EndAsync-Handler if the request should be cancelled (e.g.not processed),...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜