Difference between Abort and Interrupt in Threads in .NET

后端 未结 4 1778
攒了一身酷
攒了一身酷 2020-12-30 05:33

What is the difference between Thraed.Abort() and Thread.Interrupt(). How can I call them in a Thread Safe Manner.It would be helpful,if simple example is provided.

4条回答
  •  感动是毒
    2020-12-30 06:16

    Unless you're calling Abort or Interrupt on the currently executing thread (as ASP.NET does to terminate a request abruptly, for example) you basically can't call them in a thread-safe manner.

    Use a WaitHandle or Monitor.Wait/Pulse to wait in a wakeable way. You should only abort other threads if you're tearing down the application, basically - as otherwise you can end up in an unknown state.

    See my article on graceful thread termination for an example of how to do this nicely.

提交回复
热议问题