How to cancel an asynchronous call?

前端 未结 3 1465
长情又很酷
长情又很酷 2021-02-07 05:27

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

I have the following loop in my code which spawns multiple threads on the Threa

3条回答
  •  不知归路
    2021-02-07 06:31

    There are definitely other solutions, although I don't know that I would call them "elegant".

    you could call Abort or Interrupt on the thread but these can have some negative side effects. Personally, for something like this I prefer to use BackgroundWorker if possible. It has a Cancel feature but it is similar to what you mentioned - a bool flag in the class that you have to periodically check for in the executing code (at least it's not a global flag). This post on stopping threads in .NET is a bit old but goes over some of the pitfalls of the other options I mentioned above.

提交回复
热议问题